@@ -47,6 +47,7 @@ let compile_lc (sigma : CF.GenTypes.genTypeCategory A.sigma) (lc : LC.t) =
47
47
48
48
let rec compile_term
49
49
(sigma : CF.GenTypes.genTypeCategory A.sigma )
50
+ (ctx : GR.context )
50
51
(name : Sym.t )
51
52
(tm : GR.term )
52
53
: A. bindings
@@ -55,25 +56,22 @@ let rec compile_term
55
56
=
56
57
let loc = Locations. other __LOC__ in
57
58
match tm with
58
- | Uniform { bt; sz } ->
59
+ | Uniform { bt; sz = _ } ->
59
60
( [] ,
60
61
[] ,
61
62
A. (
62
63
mk_expr
63
64
(AilEcall
64
65
( mk_expr (AilEident (Sym. fresh_named " CN_GEN_UNIFORM" )),
65
- List. map
66
- mk_expr
67
- [ AilEident (Sym. fresh_named (name_of_bt name bt));
68
- AilEconst (ConstantInteger (IConstant (Z. of_int sz, Decimal , None )))
69
- ] ))) )
66
+ List. map mk_expr [ AilEident (Sym. fresh_named (name_of_bt name bt)) ] )))
67
+ )
70
68
| Pick { bt; choice_var; choices; last_var } ->
71
69
let var = Sym. fresh () in
72
70
let bs, ss =
73
71
List. split
74
72
(List. mapi
75
73
(fun i (_ , gr ) ->
76
- let bs, ss, e = compile_term sigma name gr in
74
+ let bs, ss, e = compile_term sigma ctx name gr in
77
75
( bs,
78
76
A. (
79
77
[ AilSexpr
@@ -131,13 +129,48 @@ let rec compile_term
131
129
[ mk_expr (AilEident choice_var) ] )))
132
130
],
133
131
A. (mk_expr (AilEident var)) )
134
- | Alloc { bytes = it } ->
135
- let alloc_sym = Sym. fresh_named " cn_gen_alloc" in
132
+ | Alloc { bytes = it ; sized } ->
133
+ let alloc_sym =
134
+ Sym. fresh_named (if sized then " CN_GEN_ALLOC_SIZED" else " CN_GEN_ALLOC" )
135
+ in
136
136
let b, s, e = compile_it sigma name it in
137
- (b, s, mk_expr (AilEcall (mk_expr (AilEident alloc_sym), [ e ])))
138
- | Call { fsym; iargs; oarg_bt; path_vars } ->
137
+ let es =
138
+ if sized then
139
+ [ e; mk_expr (AilEident (Sym. fresh_named " cn_gen_rec_size" )) ]
140
+ else
141
+ [ e ]
142
+ in
143
+ (b, s, mk_expr (AilEcall (mk_expr (AilEident alloc_sym), es)))
144
+ | Call { fsym; iargs; oarg_bt; path_vars; sized } ->
139
145
let sym = GenUtils. get_mangled_name (fsym :: List. map fst iargs) in
140
- let es = iargs |> List. map snd |> List. map (fun x -> A. (mk_expr (AilEident x))) in
146
+ let es = iargs |> List. map snd |> List. map (fun x -> A. (AilEident x)) in
147
+ let es =
148
+ List. map
149
+ mk_expr
150
+ (es
151
+ @ A. (
152
+ match sized with
153
+ | Some 1 ->
154
+ [ AilEbinary
155
+ ( mk_expr (AilEident (Sym. fresh_named " cn_gen_rec_size" )),
156
+ Arithmetic Sub ,
157
+ mk_expr
158
+ (AilEconst (ConstantInteger (IConstant (Z. one, Decimal , None )))) )
159
+ ]
160
+ | Some n ->
161
+ [ AilEbinary
162
+ ( mk_expr (AilEident (Sym. fresh_named " cn_gen_rec_size" )),
163
+ Arithmetic Div ,
164
+ mk_expr
165
+ (AilEconst
166
+ (ConstantInteger (IConstant (Z. of_int n, Decimal , None )))) )
167
+ ]
168
+ | None
169
+ when (not (GenBuiltins. is_builtin fsym))
170
+ && (ctx |> List. assoc Sym. equal fsym |> List. hd |> snd).sized ->
171
+ [ AilEcall (mk_expr (AilEident (Sym. fresh_named " cn_gen_get_size" )), [] ) ]
172
+ | None -> [] ))
173
+ in
141
174
let x = Sym. fresh () in
142
175
let b = Utils. create_binding x (bt_to_ctype fsym oarg_bt) in
143
176
let wrap_to_string (sym : Sym.t ) =
@@ -232,7 +265,7 @@ let rec compile_term
232
265
@ [ mk_expr (AilEconst ConstantNull ) ] )))
233
266
]
234
267
in
235
- let b4, s4, e4 = compile_term sigma name rest in
268
+ let b4, s4, e4 = compile_term sigma ctx name rest in
236
269
(b1 @ b2 @ b3 @ b4, s1 @ s2 @ s3 @ s4, e4)
237
270
| Let { backtracks; x; x_bt; value; last_var; rest } ->
238
271
let s1 =
@@ -250,7 +283,7 @@ let rec compile_term
250
283
] )))
251
284
]
252
285
in
253
- let b2, s2, e2 = compile_term sigma name value in
286
+ let b2, s2, e2 = compile_term sigma ctx name value in
254
287
let s3 =
255
288
A. (
256
289
[ AilSexpr
@@ -265,7 +298,13 @@ let rec compile_term
265
298
(Option. value
266
299
~default: name
267
300
(match value with
268
- | Call { fsym; iargs; oarg_bt = _ ; path_vars = _ } ->
301
+ | Call
302
+ { fsym;
303
+ iargs;
304
+ oarg_bt = _;
305
+ path_vars = _;
306
+ sized = _
307
+ } ->
269
308
Some
270
309
(GenUtils. get_mangled_name
271
310
(fsym :: List. map fst iargs))
@@ -302,7 +341,7 @@ let rec compile_term
302
341
@ [ mk_expr (AilEconst ConstantNull ) ] )))
303
342
])
304
343
in
305
- let b4, s4, e4 = compile_term sigma name rest in
344
+ let b4, s4, e4 = compile_term sigma ctx name rest in
306
345
(b2 @ [ Utils. create_binding x (bt_to_ctype name x_bt) ] @ b4, s1 @ s2 @ s3 @ s4, e4)
307
346
| Return { value } ->
308
347
let b, s, e = compile_it sigma name value in
@@ -332,12 +371,12 @@ let rec compile_term
332
371
@ [ mk_expr (AilEconst ConstantNull ) ] )))
333
372
]
334
373
in
335
- let b2, s2, e2 = compile_term sigma name rest in
374
+ let b2, s2, e2 = compile_term sigma ctx name rest in
336
375
(b1 @ b2, s1 @ s_assert @ s2, e2)
337
376
| ITE { bt; cond; t; f } ->
338
377
let b_if, s_if, e_if = compile_it sigma name cond in
339
- let b_then, s_then, e_then = compile_term sigma name t in
340
- let b_else, s_else, e_else = compile_term sigma name f in
378
+ let b_then, s_then, e_then = compile_term sigma ctx name t in
379
+ let b_else, s_else, e_else = compile_term sigma ctx name f in
341
380
let res_sym = Sym. fresh () in
342
381
let res_expr = mk_expr (AilEident res_sym) in
343
382
let res_binding = Utils. create_binding res_sym (bt_to_ctype name bt) in
@@ -406,7 +445,7 @@ let rec compile_term
406
445
(mk_expr (AilEident (Sym. fresh_named " CN_GEN_MAP_BODY" )), [ e_perm ])))
407
446
])
408
447
in
409
- let b_val, s_val, e_val = compile_term sigma name inner in
448
+ let b_val, s_val, e_val = compile_term sigma ctx name inner in
410
449
let s_end =
411
450
A. (
412
451
s_val
@@ -424,6 +463,7 @@ let rec compile_term
424
463
425
464
let compile_gen_def
426
465
(sigma : CF.GenTypes.genTypeCategory A.sigma )
466
+ (ctx : GR.context )
427
467
((name , gr ) : Sym. t * GR. definition )
428
468
: A. sigma_tag_definition * (A. sigma_declaration * 'a A. sigma_function_definition )
429
469
=
@@ -437,7 +477,12 @@ let compile_gen_def
437
477
A. Decl_function
438
478
( false ,
439
479
(C. no_qualifiers, ct_ret),
440
- List. map (fun (_ , bt ) -> (C. no_qualifiers, bt_to_ctype name bt, false )) gr.iargs,
480
+ (List. map (fun (_ , bt ) -> (C. no_qualifiers, bt_to_ctype name bt, false )) gr.iargs
481
+ @
482
+ if gr.sized then
483
+ [ (C. no_qualifiers, C. mk_ctype_integer Size_t , false ) ]
484
+ else
485
+ [] ),
441
486
false ,
442
487
false ,
443
488
false )
@@ -446,15 +491,26 @@ let compile_gen_def
446
491
let s1 =
447
492
A. (
448
493
AilSexpr
449
- (mk_expr (AilEcall (mk_expr (AilEident (Sym. fresh_named " CN_GEN_INIT" )), [] ))))
494
+ (mk_expr
495
+ (if gr.sized then
496
+ AilEcall
497
+ ( mk_expr (AilEident (Sym. fresh_named " CN_GEN_INIT_SIZED" )),
498
+ [ mk_expr (AilEident (Sym. fresh_named " cn_gen_rec_size" )) ] )
499
+ else
500
+ AilEcall (mk_expr (AilEident (Sym. fresh_named " CN_GEN_INIT" )), [] ))))
450
501
in
451
- let b2, s2, e2 = compile_term sigma name gr.body in
502
+ let b2, s2, e2 = compile_term sigma ctx name gr.body in
452
503
let sigma_def : CF.GenTypes.genTypeCategory A.sigma_function_definition =
453
504
( name,
454
505
( loc,
455
506
0 ,
456
507
CF.Annot. Attrs [] ,
457
- List. map fst gr.iargs,
508
+ (List. map fst gr.iargs
509
+ @
510
+ if gr.sized then
511
+ [ Sym. fresh_named " cn_gen_rec_size" ]
512
+ else
513
+ [] ),
458
514
mk_stmt
459
515
(A. AilSblock
460
516
( b2,
@@ -467,9 +523,7 @@ let compile_gen_def
467
523
(mk_expr
468
524
(AilEcall
469
525
( mk_expr
470
- (AilEident
471
- (Sym. fresh_named
472
- " cn_gen_backtrack_decrement_depth" )),
526
+ (AilEident (Sym. fresh_named " cn_gen_decrement_depth" )),
473
527
[] )))
474
528
]
475
529
@ A.
@@ -504,7 +558,7 @@ let compile (sigma : CF.GenTypes.genTypeCategory A.sigma) (ctx : GR.context) : P
504
558
BT. Record (List. map (fun (x , bt ) -> (Id. id (Sym. pp_string x), bt)) def.oargs)
505
559
in
506
560
CtA. augment_record_map ~cn_sym: name bt);
507
- let tag_definitions, funcs = List. split (List. map (compile_gen_def sigma) defs) in
561
+ let tag_definitions, funcs = List. split (List. map (compile_gen_def sigma ctx ) defs) in
508
562
let declarations, function_definitions = List. split funcs in
509
563
let sigma : 'a A.sigma =
510
564
{ A. empty_sigma with tag_definitions; declarations; function_definitions }
0 commit comments