-
Notifications
You must be signed in to change notification settings - Fork 147
/
C.v
673 lines (615 loc) · 35.3 KB
/
C.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
From Coq Require Import ZArith.
From Coq Require Import MSetPositive.
From Coq Require Import FMapPositive.
From Coq Require Import String.
From Coq Require Import Ascii.
From Coq Require Import Bool.
From Coq Require Import HexString.
Require Import Crypto.Util.ListUtil Coq.Lists.List.
Require Crypto.Util.Strings.String.
Require Import Crypto.Util.Strings.Decimal.
Require Import Crypto.Util.Strings.Show.
Require Import Crypto.Util.ZRange.
Require Import Crypto.Util.ZRange.Operations.
Require Import Crypto.Util.ZRange.Show.
Require Import Crypto.Util.Option.
Require Import Crypto.Util.OptionList.
Require Import Rewriter.Language.Language.
Require Import Crypto.Language.API.
Require Import Crypto.Stringification.Language.
Require Import Crypto.Stringification.IR.
Require Import Crypto.AbstractInterpretation.ZRange.
Require Import Crypto.Util.Bool.Equality.
Require Import Crypto.Util.Notations.
Import Coq.Lists.List ListNotations. Local Open Scope zrange_scope. Local Open Scope Z_scope.
Module Compilers.
Local Set Boolean Equality Schemes.
Local Set Decidable Equality Schemes.
Export Language.Compilers.
Export Language.API.Compilers.
Export AbstractInterpretation.ZRange.Compilers.
Export Stringification.Language.Compilers.
Import invert_expr.
Import Compilers.API.
Module ToString.
Import Stringification.Language.Compilers.ToString.
Import Stringification.Language.Compilers.ToString.ZRange.
Import Compilers.ToString.IR.
Local Open Scope string_scope.
Local Open Scope Z_scope.
Local Notation tZ := (base.type.type_base base.type.Z).
Module C.
Definition comment_block := List.map (fun line => "/* " ++ line ++ " */")%string.
Definition FIAT_INLINE (prefix : string) := (String.to_upper prefix ++ "FIAT_INLINE")%string.
Module String.
Definition stdint_bitwidths : list Z := [8; 16; 32; 64].
Definition is_special_bitwidth (bw : Z) := negb (existsb (Z.eqb bw) stdint_bitwidths).
Module int.
Module type.
Definition to_string_opt_typedef {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (prefix : string) (t : int.type) (typedef : option string) : string
:= int.to_string_gen_opt_typedef stdint_bitwidths "uint" "int" "_t" "" false(*type declarations not static*) false(*typedef declarations are not static*) prefix t typedef.
Definition to_string {language_naming_conventions : language_naming_conventions_opt} (prefix : string) (t : int.type) : string
:= to_string_opt_typedef (skip_typedefs:=true) prefix t None.
Definition to_literal_macro_string (t : int.type) : option string
:= if Z.ltb (int.bitwidth_of t) 8
then None
else Some ((if int.is_unsigned t then "U" else "")
++ "INT"
++ Decimal.Z.to_string (int.bitwidth_of t)
++ "_C")%string.
End type.
End int.
Module type.
Module primitive.
Definition to_string_opt_typedef {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (prefix : string) (t : type.primitive) (r : option int.type) (typedef : option string) : string
:= match r with
| Some int_t => int.type.to_string_opt_typedef prefix int_t typedef
| None => "ℤ"
end ++ match t with
| type.Zptr => "*"
| type.Z => ""
end.
Definition to_string {language_naming_conventions : language_naming_conventions_opt} (prefix : string) (t : type.primitive) (r : option int.type) : string
:= to_string_opt_typedef (skip_typedefs:=true) prefix t r None.
End primitive.
End type.
Definition value_barrier_name {language_naming_conventions : language_naming_conventions_opt} (static : bool) (prefix : string) (ty : int.type) : string
:= format_special_function_name_ty static prefix "value_barrier" ty.
Definition value_barrier_func {language_naming_conventions : language_naming_conventions_opt} (static : bool) (prefix : string) (ty : int.type) : list string
:= [""
; "#if !defined(" ++ String.to_upper prefix ++ "NO_ASM) && (defined(__GNUC__) || defined(__clang__))"
; (if static then "static " else "") ++ "__inline__ " ++ int.type.to_string prefix ty ++ " " ++ value_barrier_name static prefix ty ++ "(" ++ int.type.to_string prefix ty ++ " a) {"
; " __asm__("""" : ""+r""(a) : /* no inputs */);"
; " return a;"
; "}"
; "#else"
; "# define " ++ value_barrier_name static prefix ty ++ "(x) (x)"
; "#endif"].
Definition make_typedef
{language_naming_conventions : language_naming_conventions_opt}
{documentation_options : documentation_options_opt}
(prefix : string)
(typedef : typedef_info)
: list string
:= let '(name, (ty, array_len), description) := name_and_type_and_describe_typedef prefix false(*type declarations not static*) typedef in
((comment_block description)
++ match array_len with
| None (* just an integer *)
=> ["typedef " ++ String.type.primitive.to_string prefix type.Z ty ++ " " ++ name ++ ";"]
| Some None (* unknown array length *)
=> ["typedef " ++ String.type.primitive.to_string prefix type.Zptr ty ++ " " ++ name ++ ";"]
| Some (Some len)
=> ["typedef " ++ String.type.primitive.to_string prefix type.Z ty ++ " " ++ name ++ "[" ++ Decimal.Z.to_string (Z.of_nat len) ++ "];"]
end%string)%list.
Definition header
{language_naming_conventions : language_naming_conventions_opt}
{documentation_options : documentation_options_opt}
{package_namev : package_name_opt}
{class_namev : class_name_opt}
{output_options : output_options_opt}
(machine_wordsize : Z) (internal_static : bool) (static : bool) (prefix : string) (infos : ident_infos)
(typedef_map : list typedef_info)
: list string
:= let bitwidths_used := bitwidths_used infos in
let value_barrier_bitwidths := value_barrier_bitwidths infos in
let FIAT_EXTENSION := (String.to_upper prefix ++ "FIAT_EXTENSION")%string in
(["";
"#include <stdint.h>"]
++ (if IntSet.mem _Bool bitwidths_used || IntSet.mem (int.signed_counterpart_of _Bool) bitwidths_used
then ["typedef unsigned char " ++ int.type.to_string prefix _Bool ++ ";";
"typedef signed char " ++ int.type.to_string prefix (int.signed_counterpart_of _Bool) ++ ";"]%string
else [])
++ (let gnuc_defines
:= (if IntSet.mem uint128 bitwidths_used || IntSet.mem int128 bitwidths_used
then [(FIAT_EXTENSION, "__extension__")]
else [])
++ [(FIAT_INLINE prefix, "__inline__")]
in
["#if defined(__GNUC__) || defined(__clang__)"]
++ List.map (fun '(MACRO, primitive) => "# define " ++ MACRO ++ " " ++ primitive)%string gnuc_defines
++ ["#else"]
++ List.map (fun '(MACRO, primitive) => "# define " ++ MACRO)%string gnuc_defines
++ ["#endif"])
++ (if IntSet.mem uint128 bitwidths_used || IntSet.mem int128 bitwidths_used
then ["";
FIAT_EXTENSION ++ " typedef signed __int128 " ++ int.type.to_string prefix int128 ++ ";";
FIAT_EXTENSION ++ " typedef unsigned __int128 " ++ int.type.to_string prefix uint128 ++ ";"]%string
else [])
++ (if skip_typedefs
then []
else List.flat_map
(fun td_name =>
match List.find (fun '(name, _, _, _) => (td_name =? name)%string) typedef_map with
| Some td_info => [""] ++ make_typedef prefix td_info
| None => ["#error ""Could not find typedef info for '" ++ td_name ++ "'"";"]%string
end%list)
(typedefs_used infos))
++ [""
; "#if (-1 & 3) != 3"
; "#error ""This code only works on a two's complement system"""
; "#endif"]
++ (List.flat_map
(value_barrier_func internal_static prefix)
(IntSet.elements value_barrier_bitwidths))
++ [""])%list.
End String.
Module primitive.
Definition to_string (prefix : string) (t : type.primitive) (v : BinInt.Z) : string
:= match t, String.int.type.to_literal_macro_string (int.of_zrange_relaxed r[v ~> v]) with
| type.Z, Some macro
=> macro ++ "(" ++ HexString.of_Z v ++ ")"
| type.Z, None => HexString.of_Z v
| type.Zptr, _ => "#error ""literal address " ++ HexString.of_Z v ++ """;"
end.
End primitive.
Fixpoint arith_to_string
{language_naming_conventions : language_naming_conventions_opt} (internal_static : bool)
(prefix : string) {t} (e : arith_expr t) : string
:= let special_name_ty name ty := format_special_function_name_ty internal_static prefix name ty in
let special_name name bw := format_special_function_name internal_static prefix name false(*unsigned*) bw in
match e with
| (literal v @@@ _) => primitive.to_string prefix type.Z v
| (List_nth n @@@ Var _ v)
=> "(" ++ v ++ "[" ++ Decimal.Z.to_string (Z.of_nat n) ++ "])"
| (Addr @@@ Var _ v) => "&" ++ v
| (Dereference @@@ e) => "( *" ++ arith_to_string internal_static prefix e ++ " )"
| (Z_shiftr offset @@@ e)
=> "(" ++ arith_to_string internal_static prefix e ++ " >> " ++ Decimal.Z.to_string offset ++ ")"
| (Z_shiftl offset @@@ e)
=> "(" ++ arith_to_string internal_static prefix e ++ " << " ++ Decimal.Z.to_string offset ++ ")"
| (Z_land @@@ (e1, e2))
=> "(" ++ arith_to_string internal_static prefix e1 ++ " & " ++ arith_to_string internal_static prefix e2 ++ ")"
| (Z_lor @@@ (e1, e2))
=> "(" ++ arith_to_string internal_static prefix e1 ++ " | " ++ arith_to_string internal_static prefix e2 ++ ")"
| (Z_lxor @@@ (e1, e2))
=> "(" ++ arith_to_string internal_static prefix e1 ++ " ^ " ++ arith_to_string internal_static prefix e2 ++ ")"
| (Z_add @@@ (x1, x2))
=> "(" ++ arith_to_string internal_static prefix x1 ++ " + " ++ arith_to_string internal_static prefix x2 ++ ")"
| (Z_mul @@@ (x1, x2))
=> "(" ++ arith_to_string internal_static prefix x1 ++ " * " ++ arith_to_string internal_static prefix x2 ++ ")"
| (Z_sub @@@ (x1, x2))
=> "(" ++ arith_to_string internal_static prefix x1 ++ " - " ++ arith_to_string internal_static prefix x2 ++ ")"
| (Z_lnot _ @@@ e)
=> "(~" ++ arith_to_string internal_static prefix e ++ ")"
| (Z_bneg @@@ e)
=> "(!" ++ arith_to_string internal_static prefix e ++ ")"
| (Z_value_barrier ty @@@ e)
=> String.value_barrier_name internal_static prefix ty ++ "(" ++ arith_to_string internal_static prefix e ++ ")"
| (Z_mul_split lg2s @@@ args)
=> special_name "mulx" lg2s ++ "(" ++ arith_to_string internal_static prefix args ++ ")"
| (Z_add_with_get_carry lg2s @@@ args)
=> special_name "addcarryx" lg2s ++ "(" ++ arith_to_string internal_static prefix args ++ ")"
| (Z_sub_with_get_borrow lg2s @@@ args)
=> special_name "subborrowx" lg2s ++ "(" ++ arith_to_string internal_static prefix args ++ ")"
| (Z_zselect ty @@@ args)
=> special_name_ty "cmovznz" ty ++ "(" ++ arith_to_string internal_static prefix args ++ ")"
| (Z_add_modulo @@@ (x1, x2, x3)) => "#error addmodulo;"
| (Z_static_cast int_t @@@ e)
=> "(" ++ String.type.primitive.to_string prefix type.Z (Some int_t) ++ ")"
++ arith_to_string internal_static prefix e
| Var _ v => v
| Pair A B a b
=> arith_to_string internal_static prefix a ++ ", " ++ arith_to_string internal_static prefix b
| (List_nth _ @@@ _)
| (Addr @@@ _)
| (Z_add @@@ _)
| (Z_mul @@@ _)
| (Z_sub @@@ _)
| (Z_land @@@ _)
| (Z_lor @@@ _)
| (Z_lxor @@@ _)
| (Z_add_modulo @@@ _)
=> "#error bad_arg;"
| TT
=> "#error tt;"
end%core%Cexpr.
Definition stmt_to_string
{language_naming_conventions : language_naming_conventions_opt} (internal_static : bool)
(prefix : string)
(e : stmt)
: string
:= match e with
| Call val
=> arith_to_string internal_static prefix val ++ ";"
| Assign true t sz name val
=> String.type.primitive.to_string prefix t sz ++ " " ++ name ++ " = " ++ arith_to_string internal_static prefix val ++ ";"
| Assign false _ sz name val
=> name ++ " = " ++ arith_to_string internal_static prefix val ++ ";"
| AssignZPtr name sz val
=> "*" ++ name ++ " = " ++ arith_to_string internal_static prefix val ++ ";"
| DeclareVar t sz name
=> String.type.primitive.to_string prefix t sz ++ " " ++ name ++ ";"
| Comment lines _
=> String.concat String.NewLine (comment_block (ToString.preprocess_comment_block lines))
| AssignNth name n val
=> name ++ "[" ++ Decimal.Z.to_string (Z.of_nat n) ++ "] = " ++ arith_to_string internal_static prefix val ++ ";"
end.
Definition to_strings
{language_naming_conventions : language_naming_conventions_opt} (internal_static : bool)
(prefix : string)
(e : expr)
: list string
:= List.map (stmt_to_string internal_static prefix) e.
Import OfPHOAS.
Fixpoint to_base_arg_list {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (prefix : string) {t} : base_var_data t -> list string
:= match t return base_var_data t -> _ with
| tZ
=> fun '(n, is_ptr, r, typedef)
=> [String.type.primitive.to_string_opt_typedef prefix type.Z r typedef ++ " " ++ n]
| base.type.prod A B
=> fun '(va, vb) => (to_base_arg_list prefix va ++ to_base_arg_list prefix vb)%list
| base.type.list tZ
=> fun '(n, r, len, typedef)
=> ["const "
++ match (if skip_typedefs then None else typedef) with
| Some typedef => format_typedef_name prefix false(*type declarations not static*) typedef ++ " " ++ n
| None => String.type.primitive.to_string prefix type.Z r ++ " " ++ n ++ "[" ++ Decimal.Z.to_string (Z.of_nat len) ++ "]"
end]
| base.type.list _ => fun _ => ["#error ""complex list"";"]
| base.type.option _ => fun _ => ["#error option;"]
| base.type.unit => fun _ => ["#error unit;"]
| base.type.type_base t => fun _ => ["#error " ++ show t ++ ";"]%string
end.
Definition to_arg_list {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (prefix : string) {t} : var_data t -> list string
:= match t return var_data t -> _ with
| type.base t => to_base_arg_list prefix
| type.arrow _ _ => fun _ => ["#error arrow;"]
end.
Fixpoint to_arg_list_for_each_lhs_of_arrow {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (prefix : string) {t} : type.for_each_lhs_of_arrow var_data t -> list string
:= match t return type.for_each_lhs_of_arrow var_data t -> _ with
| type.base t => fun _ => nil
| type.arrow s d
=> fun '(x, xs)
=> to_arg_list prefix x ++ to_arg_list_for_each_lhs_of_arrow prefix xs
end%list.
Fixpoint to_base_retarg_list {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} prefix {t} : base_var_data t -> list string
:= match t return base_var_data t -> _ with
| tZ
=> fun '(n, is_ptr, r, typedef)
=> [match typedef with
| Some typedef => format_typedef_name prefix false(*type declarations not static*) typedef ++ "*"
| None => String.type.primitive.to_string prefix type.Zptr r
end
++ " " ++ n]
| base.type.prod A B
=> fun '(va, vb) => (to_base_retarg_list prefix va ++ to_base_retarg_list prefix vb)%list
| base.type.list tZ
=> fun '(n, r, len, typedef)
=> [match (if skip_typedefs then None else typedef) with
| Some typedef => format_typedef_name prefix false(*type declarations not static*) typedef ++ " " ++ n
| None => String.type.primitive.to_string prefix type.Z r ++ " " ++ n ++ "[" ++ Decimal.Z.to_string (Z.of_nat len) ++ "]"
end]
| base.type.list _ => fun _ => ["#error ""complex list"";"]
| base.type.option _ => fun _ => ["#error option;"]
| base.type.unit => fun _ => ["#error unit;"]
| base.type.type_base t => fun _ => ["#error " ++ show t ++ ";"]%string
end.
Definition to_retarg_list {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (prefix : string) {t} : var_data t -> list string
:= match t return var_data t -> _ with
| type.base _ => to_base_retarg_list prefix
| type.arrow _ _ => fun _ => ["#error arrow;"]
end.
(** * Language-specific numeric conversions to be passed to the PHOAS -> IR translation *)
(** Quoting
http://en.cppreference.com/w/c/language/conversion:
** Integer promotions
Integer promotion is the implicit conversion of a value of
any integer type with rank less or equal to rank of int or
of a bit field of type _Bool, int, signed int, unsigned
int, to the value of type int or unsigned int
If int can represent the entire range of values of the
original type (or the range of values of the original bit
field), the value is converted to type int. Otherwise the
value is converted to unsigned int. *)
(** We assume a 32-bit [int] type *)
Definition integer_promote_type (t : int.type) : int.type
:= if int.is_tighter_than t int32
then int32
else t.
(** Quoting
http://en.cppreference.com/w/c/language/conversion:
rank above is a property of every integer type and is
defined as follows:
1) the ranks of all signed integer types are different and
increase with their precision: rank of signed char <
rank of short < rank of int < rank of long int < rank
of long long int
2) the ranks of all signed integer types equal the ranks
of the corresponding unsigned integer types
3) the rank of any standard integer type is greater than
the rank of any extended integer type of the same size
(that is, rank of __int64 < rank of long long int, but
rank of long long < rank of __int128 due to the rule
(1))
4) rank of char equals rank of signed char and rank of
unsigned char
5) the rank of _Bool is less than the rank of any other
standard integer type
6) the rank of any enumerated type equals the rank of its
compatible integer type
7) ranking is transitive: if rank of T1 < rank of T2 and
rank of T2 < rank of T3 then rank of T1 < rank of T3
8) any aspects of relative ranking of extended integer
types not covered above are implementation defined *)
(** We define the rank to be the bitwidth, which satisfies
(1), (2), (4), (5), and (7). Points (3) and (6) do not
apply. *)
Definition rank (r : int.type) : BinInt.Z := int.bitwidth_of r.
Definition IMPOSSIBLE {T} (v : T) : T. exact v. Qed.
(** Quoting
http://en.cppreference.com/w/c/language/conversion: *)
Definition C_common_type (t1 t2 : int.type) : int.type
(** First of all, both operands undergo integer promotions
(see below). Then *)
:= let t1 := integer_promote_type t1 in
let t2 := integer_promote_type t2 in
(** - If the types after promotion are the same, that
type is the common type *)
if int.type_beq t1 t2 then
t1
(** - Otherwise, if both operands after promotion have
the same signedness (both signed or both unsigned),
the operand with the lesser conversion rank (see
below) is implicitly converted to the type of the
operand with the greater conversion rank *)
else if bool_beq (int.is_signed t1) (int.is_signed t2) then
(if rank t1 >=? rank t2 then t1 else t2)
(** - Otherwise, the signedness is different: If the
operand with the unsigned type has conversion rank
greater or equal than the rank of the type of the
signed operand, then the operand with the signed
type is implicitly converted to the unsigned type
*)
else if int.is_unsigned t1 && (rank t1 >=? rank t2) then
t1
else if int.is_unsigned t2 && (rank t2 >=? rank t1) then
t2
(** - Otherwise, the signedness is different and the
signed operand's rank is greater than unsigned
operand's rank. In this case, if the signed type
can represent all values of the unsigned type, then
the operand with the unsigned type is implicitly
converted to the type of the signed operand. *)
else if int.is_signed t1 && int.is_tighter_than t2 t1 then
t1
else if int.is_signed t2 && int.is_tighter_than t1 t2 then
t2
(** - Otherwise, both operands undergo implicit
conversion to the unsigned type counterpart of the
signed operand's type. *)
(** N.B. This case ought to be impossible in our code,
where [rank] is the bitwidth. *)
else if int.is_signed t1 then
int.unsigned_counterpart_of t1
else
int.unsigned_counterpart_of t2.
(** Quoting https://en.cppreference.com/w/c/language/conversion: *)
(** Usual arithmetic conversions
The arguments of the following arithmetic operators undergo
implicit conversions for the purpose of obtaining the common
real type, which is the type in which the calculation is
performed:
- binary arithmetic *, /, %, +, -
- relational operators <, >, <=, >=, ==, !=
- binary bitwise arithmetic &, ^, |,
- the conditional operator ?: *)
Inductive binop_kind := use_common_type.
Definition kind_of_binop (idc : Z_binop) : binop_kind
:= match idc with
| Z_land
| Z_lor
| Z_lxor
| Z_add
| Z_mul
| Z_sub
=> use_common_type
end.
Definition C_bin_op_natural_output
: Z_binop -> int.type * int.type -> int.type
:= fun idc '(t1, t2)
=> match kind_of_binop idc with
| use_common_type => C_common_type t1 t2
end.
Definition C_bin_op_casts
: Z_binop -> option int.type -> int.type * int.type -> option int.type * (option int.type * option int.type)
:= fun idc desired_type '(t1, t2)
=> match kind_of_binop idc with
| use_common_type
=> match desired_type with
| None => (None, (None, None))
| Some _
=> let t1 := integer_promote_type t1 in
let t2 := integer_promote_type t2 in
let '(t1o, t2o)
:= if int.is_tighter_than t2 t1
then (get_Zcast_up_if_needed desired_type (Some t1), None)
else (None, get_Zcast_up_if_needed desired_type (Some t2)) in
let ct := C_common_type (Option.value t1o t1) (Option.value t2o t2) in
(get_Zcast_down_if_needed desired_type (Some ct), (t1o, t2o))
end
end.
Definition C_un_op_casts
: Z_unop -> option int.type -> int.type -> option int.type * option int.type
:= fun idc desired_type t
=> let t := integer_promote_type t in
match idc with
| Z_shiftr offset
=> (** N.B. We must cast the expression up to a large
enough type to fit 2^offset (importantly, not
just 2^offset-1), because C considers it to be
undefined behavior to shift >= width of the
type. We should probably figure out how to not
generate these things in the first place...
N.B. We must preserve signedness of the value
being shifted, because shift does not commute
with mod. *)
let t' := int.union_zrange r[0~>2^offset]%zrange t in
((** We cast the result down to the specified type, if needed *)
get_Zcast_down_if_needed desired_type (Some t'),
(** We cast the argument up to a large enough type *)
get_Zcast_up_if_needed (Some t') (Some t))
| Z_shiftl offset
=> (** N.B. We must cast the expression up to a large
enough type to fit 2^offset (importantly, not
just 2^offset-1), because C considers it to be
undefined behavior to shift >= width of the
type. We should probably figure out how to not
generate these things in the first place...
N.B. We make sure that we only left-shift
unsigned values, since shifting into the sign
bit is undefined behavior. *)
let rpre_out := match desired_type with
| Some rout => Some (int.union_zrange r[0~>2^offset] (int.unsigned_counterpart_of rout))
| None => Some (int.of_zrange_relaxed r[0~>2^offset]%zrange)
end in
((** We cast the result down to the specified type, if needed *)
get_Zcast_down_if_needed desired_type rpre_out,
(** We cast the argument up to a large enough type *)
get_Zcast_up_if_needed rpre_out (Some t))
| Z_lnot ty
=> ((* if the result is too big, we cast it down; we
don't need to upcast it because it'll get
picked up by implicit casts if necessary *)
get_Zcast_down_if_needed desired_type (Some ty),
(** always cast to the width of the type, unless we are already exactly that type (which the machinery in IR handles) *)
Some ty)
| Z_value_barrier ty
=> ((* if the result is too big, we cast it down; we
don't need to upcast it because it'll get
picked up by implicit casts if necessary *)
get_Zcast_down_if_needed desired_type (Some ty),
(** implicit casts will cast the argument up if needed *)
None)
| Z_bneg
=> ((* bneg is !, i.e., takes the argument to 1 if its not zero, and to zero if it is zero; so we don't ever need to cast *)
None, None)
end.
Local Instance CLanguageCasts : LanguageCasts :=
{| bin_op_natural_output := C_bin_op_natural_output
; bin_op_casts := C_bin_op_casts
; un_op_casts := C_un_op_casts
; upcast_on_assignment := false
; upcast_on_funcall := false
; explicit_pointer_variables := false
|}.
(** Top-level printing functions *)
Definition to_function_lines
{language_naming_conventions : language_naming_conventions_opt}
{skip_typedefs : skip_typedefs_opt}
(internal_static : bool) (static : bool) (all_static : bool) (inline : bool) (prefix : string) (name : string)
{t}
(f : type.for_each_lhs_of_arrow var_data t * var_data (type.base (type.final_codomain t)) * expr)
: list string
:= let '(args, rets, body) := f in
(((((if static then "static " else "")
++ (if inline then FIAT_INLINE prefix ++ " " else "")
++ "void "
++ name ++ "("
++ (String.concat ", " (to_retarg_list prefix rets ++ to_arg_list_for_each_lhs_of_arrow prefix args))
++ ") {")%string)
:: (List.map (fun s => " " ++ s)%string (to_strings internal_static prefix body)))
++ ["}"])%list.
(** In C, there is no munging of return arguments (they remain
passed by pointers), so all variables are live *)
Local Instance : consider_retargs_live_opt := fun _ _ _ => true.
Local Instance : rename_dead_opt := fun s => s.
(** In C we do want to lift declarations to the top, to comply
with ISO C90 *)
Local Instance : lift_declarations_opt := true.
Definition ToFunctionLines
{absint_opts : AbstractInterpretation.Options}
{relax_zrange : relax_zrange_opt}
{language_naming_conventions : language_naming_conventions_opt}
{documentation_options : documentation_options_opt}
{output_options : output_options_opt}
(machine_wordsize : Z)
(do_bounds_check : bool) (internal_static : bool) (static : bool) (all_static : bool) (inline : bool) (prefix : string) (name : string)
{t}
(e : @Compilers.expr.Expr base.type ident.ident t)
(comment : type.for_each_lhs_of_arrow var_data t -> var_data (type.base (type.final_codomain t)) -> list string)
(name_list : option (list string))
(inbounds : type.for_each_lhs_of_arrow ZRange.type.option.interp t)
(outbounds : ZRange.type.base.option.interp (type.final_codomain t))
(intypedefs : type.for_each_lhs_of_arrow var_typedef_data t)
(outtypedefs : base_var_typedef_data (type.final_codomain t))
: (list string * ident_infos) + string
:= match ExprOfPHOAS do_bounds_check e name_list inbounds intypedefs outtypedefs with
| inl (indata, outdata, f)
=> inl ((["/*"]
++ (List.map (fun s => if (String.length s =? 0)%nat then " *" else (" * " ++ s))%string (comment indata outdata))
++ match input_bounds_to_string indata inbounds with
| nil => nil
| ls => [" * Input Bounds:"] ++ List.map (fun v => " * " ++ v)%string ls
end
++ match bound_to_string outdata outbounds with
| nil => nil
| ls => [" * Output Bounds:"] ++ List.map (fun v => " * " ++ v)%string ls
end
++ [" */"]
++ to_function_lines internal_static static all_static inline prefix name (indata, outdata, f))%list,
ident_infos.collect_all_infos f intypedefs outtypedefs)
| inr nil
=> inr ("Unknown internal error in converting " ++ name ++ " to C")%string
| inr [err]
=> inr ("Error in converting " ++ name ++ " to C:" ++ String.NewLine ++ err)%string
| inr errs
=> inr ("Errors in converting " ++ name ++ " to C:" ++ String.NewLine ++ String.concat String.NewLine errs)%string
end.
Definition ToFunctionString
{absint_opts : AbstractInterpretation.Options}
{relax_zrange : relax_zrange_opt}
{language_naming_conventions : language_naming_conventions_opt}
{documentation_options : documentation_options_opt}
{output_options : output_options_opt}
(machine_wordsize : Z)
(do_bounds_check : bool) (internal_static : bool) (static : bool) (all_static : bool) (inline : bool) (prefix : string) (name : string)
{t}
(e : @Compilers.expr.Expr base.type ident.ident t)
(comment : type.for_each_lhs_of_arrow var_data t -> var_data (type.base (type.final_codomain t)) -> list string)
(name_list : option (list string))
(inbounds : type.for_each_lhs_of_arrow ZRange.type.option.interp t)
(outbounds : ZRange.type.option.interp (type.base (type.final_codomain t)))
(intypedefs : type.for_each_lhs_of_arrow var_typedef_data t)
(outtypedefs : base_var_typedef_data (type.final_codomain t))
: (string * ident_infos) + string
:= match ToFunctionLines machine_wordsize do_bounds_check internal_static static all_static inline prefix name e comment name_list inbounds outbounds intypedefs outtypedefs with
| inl (ls, used_types) => inl (LinesToString ls, used_types)
| inr err => inr err
end.
Definition OutputCAPI : OutputLanguageAPI :=
{|
ToString.comment_block := comment_block;
ToString.comment_file_header_block := comment_block;
ToString.ToFunctionLines := @ToFunctionLines;
ToString.header := @String.header;
ToString.footer := fun _ _ _ _ _ _ _ _ _ => [];
(** We handle value_barrier specially *)
ToString.strip_special_infos machine_wordsize infos
:= ident_info_with_value_barrier infos IntSet.empty;
|}.
End C.
Notation ToFunctionLines := C.ToFunctionLines.
Notation ToFunctionString := C.ToFunctionString.
Notation OutputCAPI := C.OutputCAPI.
End ToString.
End Compilers.