Source file controlflow_build.ml
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
open Common
open Ast_generic
module Ast = Ast_generic
module F = Controlflow
type state = {
g: F.flow;
exiti: F.nodei;
ctx: context Common.stack;
}
and context =
| NoCtx
| LoopCtx of F.nodei * F.nodei
| SwitchCtx of F.nodei
| TryCtx of F.nodei
type error = error_kind * Parse_info.t option
and error_kind =
| NoEnclosingLoop
| DynamicBreak
| UnreachableStatement of Controlflow.node_kind
exception Error of error
let verbose = ref false
let stmts_of_stmt_or_defs xs =
xs |> Common.map_filter (fun stmt_or_def ->
match stmt_or_def with
| DefStmt (_, VarDef _) -> Some stmt_or_def
| DefStmt _
| DirectiveStmt _ ->
if !verbose
then pr2_once ("ignoring nested func/class/directive in CFG");
None
| st -> Some st
)
let add_arc (starti, nodei) g =
g#add_arc ((starti, nodei), F.Direct)
let add_arc_opt (starti_opt, nodei) g =
starti_opt |> Common.do_option (fun starti ->
g#add_arc ((starti, nodei), F.Direct)
)
let (lookup_some_ctx:
?level:int ->
ctx_filter:(context -> 'a option) ->
context list -> 'a option) =
fun ?(level=1) ~ctx_filter xs ->
let rec aux depth xs =
match xs with
| [] -> None
| x::xs ->
(match ctx_filter x with
| None -> aux depth xs
| Some a ->
if depth = level
then (Some a)
else
aux (depth+1) xs
)
in
aux 1 xs
let info_opt any =
match Lib_ast.ii_of_any any with
| [] -> None
| x::_xs -> Some x
let rec (cfg_stmt: state -> F.nodei option -> stmt -> F.nodei option) =
fun state previ stmt ->
let i () = info_opt (S stmt) in
match stmt with
| Label _ | Goto _ -> raise Todo
| ExprStmt e ->
cfg_expr state previ e
| Block xs ->
let stmts = stmts_of_stmt_or_defs (xs) in
cfg_stmt_list state previ stmts
| For _ | While _ ->
let node, stmt =
(match stmt with
| While (e, stmt) ->
F.WhileHeader (e), stmt
| For (_forheader, stmt) ->
F.ForHeader, stmt
| _ -> raise Impossible
)
in
let newi = state.g#add_node { F.n = node; i=i() } in
state.g |> add_arc_opt (previ, newi);
let newfakethen = state.g#add_node { F.n = F.TrueNode;i=None } in
let newfakeelse = state.g#add_node { F.n = F.FalseNode;i=None } in
state.g |> add_arc (newi, newfakethen);
state.g |> add_arc (newi, newfakeelse);
let state = { state with
ctx = LoopCtx (newi, newfakeelse)::state.ctx;
}
in
let finalthen = cfg_stmt state (Some newfakethen) stmt in
state.g |> add_arc_opt (finalthen, newi);
Some newfakeelse
| DoWhile (st, e) ->
let doi = state.g#add_node { F.n = F.DoHeader;i=i() } in
state.g |> add_arc_opt (previ, doi);
let taili = state.g#add_node
{ F.n = F.DoWhileTail (e);i=None } in
let newfakethen = state.g#add_node { F.n = F.TrueNode;i=None } in
let newfakeelse = state.g#add_node { F.n = F.FalseNode;i=None } in
state.g |> add_arc (taili, newfakethen);
state.g |> add_arc (taili, newfakeelse);
state.g |> add_arc (newfakethen, doi);
let state = { state with
ctx = LoopCtx (taili, newfakeelse)::state.ctx;
}
in
let finalthen = cfg_stmt state (Some doi) st in
(match finalthen with
| None ->
None
| Some finalthen ->
state.g |> add_arc (finalthen, taili);
Some newfakeelse
)
| If (e, st_then, st_else) ->
let newi = state.g#add_node { F.n = F.IfHeader (e);i=i() } in
state.g |> add_arc_opt (previ, newi);
let newfakethen = state.g#add_node { F.n = F.TrueNode;i=None } in
let newfakeelse = state.g#add_node { F.n = F.FalseNode;i=None } in
state.g |> add_arc (newi, newfakethen);
state.g |> add_arc (newi, newfakeelse);
let finalthen = cfg_stmt state (Some newfakethen) st_then in
let finalelse = cfg_stmt state (Some newfakeelse) st_else in
(match finalthen, finalelse with
| None, None ->
None
| Some nodei, None
| None, Some nodei ->
Some nodei
| Some n1, Some n2 ->
let lasti = state.g#add_node { F.n = F.Join;i=None } in
state.g |> add_arc (n1, lasti);
state.g |> add_arc (n2, lasti);
Some lasti
)
| Return (e) ->
let newi = state.g#add_node { F.n = F.Return e;i=i() } in
state.g |> add_arc_opt (previ, newi);
state.g |> add_arc (newi, state.exiti);
None
| Continue (eopt) | Break (eopt) ->
let is_continue, node =
match stmt with
| Continue _ -> true, F.Continue eopt
| Break _ -> false, F.Break eopt
| _ -> raise Impossible
in
let newi = state.g#add_node { F.n = node;i=i() } in
state.g |> add_arc_opt (previ, newi);
let nodei_to_jump_to =
state.ctx |> lookup_some_ctx
~level:1
~ctx_filter:(function
| LoopCtx (headi, endi) ->
if is_continue
then Some (headi)
else Some (endi)
| SwitchCtx (endi) ->
Some endi
| TryCtx _ | NoCtx -> None
)
in
(match nodei_to_jump_to with
| Some nodei ->
state.g |> add_arc (newi, nodei);
| None ->
raise (Error (NoEnclosingLoop, i()))
);
None
| Switch (e, cases_and_body) ->
let newi = state.g#add_node
{ F.n = F.SwitchHeader (e);i=i() } in
state.g |> add_arc_opt (previ, newi);
let endi = state.g#add_node { F.n = F.SwitchEnd;i=None } in
if (not (cases_and_body |> List.exists (fun (cases, _body) ->
cases |> List.exists (function
| Ast.Default -> true | _ -> false))))
then begin
state.g |> add_arc (newi, endi);
end;
let last_stmt_opt =
cfg_cases (newi, endi) state (None) cases_and_body
in
state.g |> add_arc_opt (last_stmt_opt, endi);
if (state.g#predecessors endi)#null then begin
state.g#del_node endi;
None
end else
Some endi
| Try(body, catches, _finallys) ->
let newi = state.g#add_node { F.n = F.TryHeader;i=i() } in
let catchi = state.g#add_node { F.n = F.CatchStart;i=None } in
state.g |> add_arc_opt (previ, newi);
let endi = state.g#add_node { F.n = F.TryEnd;i=None } in
state.g |> add_arc (newi, catchi);
let state' = { state with
ctx = TryCtx (catchi)::state.ctx;
}
in
let last_stmt_opt = cfg_stmt state' (Some newi) body in
state.g |> add_arc_opt (last_stmt_opt, endi);
let last_false_node =
cfg_catches state catchi endi (catches) in
let nodei_to_jump_to =
state.ctx |> lookup_some_ctx ~ctx_filter:(function
| TryCtx (nextcatchi) -> Some nextcatchi
| LoopCtx _ | SwitchCtx _ | NoCtx -> None
)
in
(match nodei_to_jump_to with
| Some nextcatchi ->
state.g |> add_arc (last_false_node, nextcatchi)
| None ->
state.g |> add_arc (last_false_node, state.exiti)
);
if (state.g#predecessors endi)#null then begin
state.g#del_node endi;
None
end
else
Some endi
| Throw (e) ->
let newi = state.g#add_node { F.n = F.Throw e; i=i() } in
state.g |> add_arc_opt (previ, newi);
let nodei_to_jump_to =
state.ctx |> lookup_some_ctx
~ctx_filter:(function
| TryCtx (catchi) ->
Some catchi
| LoopCtx _ | SwitchCtx _ | NoCtx ->
None
)
in
(match nodei_to_jump_to with
| Some catchi ->
state.g |> add_arc (newi, catchi)
| None ->
state.g |> add_arc (newi, state.exiti)
);
None
| Assert _
| OtherStmt _
->
let simple_stmt = F.TodoSimpleStmt in
let newi = state.g#add_node { F.n = F.SimpleStmt simple_stmt;i=i() } in
state.g |> add_arc_opt (previ, newi);
Some newi
| DefStmt (_ent, VarDef _def) ->
raise Todo
| DefStmt _
| DirectiveStmt _ ->
raise Impossible
and cfg_stmt_list state previ xs =
xs |> List.fold_left (fun previ stmt ->
cfg_stmt state previ stmt
) previ
and (cfg_cases:
(F.nodei * F.nodei) -> state ->
F.nodei option -> Ast.case_and_body list -> F.nodei option) =
fun (switchi, endswitchi) state previ cases ->
let state = { state with
ctx = SwitchCtx (endswitchi)::state.ctx;
}
in
cases |> List.fold_left (fun previ case_and_body ->
let (cases, stmt) = case_and_body in
let node =
match cases with
| [Default] -> F.Default
| _ -> F.Case
in
let i () = info_opt (S stmt) in
let newi = state.g#add_node { F.n = node; i=i() } in
state.g |> add_arc_opt (previ, newi);
state.g |> add_arc (switchi, newi);
cfg_stmt state (Some newi) stmt
) previ
and (cfg_catches: state -> F.nodei -> F.nodei -> Ast.catch list -> F.nodei) =
fun state previ tryendi catches ->
catches |> List.fold_left (fun previ catch ->
let (_pattern, stmt) = catch in
let i () = info_opt (S stmt) in
let newi = state.g#add_node { F.n = F.Catch; i= i() } in
state.g |> add_arc (previ, newi);
let ei = Some newi in
let truei = state.g#add_node { F.n = F.TrueNode;i=None } in
let falsei = state.g#add_node { F.n = F.FalseNode;i=None } in
state.g |> add_arc_opt (ei, truei);
state.g |> add_arc_opt (ei, falsei);
let last_stmt_opt = cfg_stmt state (Some truei) stmt in
state.g |> add_arc_opt (last_stmt_opt, tryendi);
falsei
) previ
and cfg_expr state previ expr =
let i = info_opt (E expr) in
let newi = state.g#add_node {
F.n = F.SimpleStmt (F.ExprStmt (expr));
i=i
} in
state.g |> add_arc_opt (previ, newi);
Some newi
let (control_flow_graph_of_stmts: parameter list -> stmt list -> F.flow) =
fun params xs ->
let g = new Ograph_extended.ograph_mutable in
let enteri = g#add_node { F.n = F.Enter;i=None } in
let exiti = g#add_node { F.n = F.Exit; i=None } in
let newi = params |> List.fold_left (fun previ param ->
let parami = g#add_node { F.n = F.Parameter param; i=None } in
g |> add_arc (previ, parami);
parami
) enteri
in
let state = {
g = g;
exiti = exiti;
ctx = [NoCtx];
}
in
let last_node_opt =
cfg_stmt_list state (Some newi) xs
in
g |> add_arc_opt (last_node_opt, exiti);
g
let (cfg_of_func: function_definition -> F.flow) = fun def ->
let params = def.fparams in
control_flow_graph_of_stmts params [def.fbody]
let cfg_of_stmts = control_flow_graph_of_stmts
let (deadcode_detection : F.flow -> unit) = fun flow ->
flow#nodes#iter (fun (k, node) ->
let pred = flow#predecessors k in
if pred#null then
(match node.F.n with
| F.Enter -> ()
| _ ->
(match node.F.i with
| None ->
pr2 (spf "CFG: PB, found dead node but no loc: %s"
(Controlflow.short_string_of_node node))
| Some info ->
raise (Error (UnreachableStatement node.F.n, Some info))
)
)
)
let string_of_error_kind error_kind =
match error_kind with
| UnreachableStatement (node_kind) ->
"Unreachable statement detected " ^
(F.short_string_of_node_kind node_kind)
| NoEnclosingLoop ->
"No enclosing loop found for break or continue"
| DynamicBreak ->
"Dynamic break/continue are not supported"
let string_of_error (error_kind, info) =
match info with
| None -> spf "NOLOC: FLOW %s" (string_of_error_kind error_kind)
| Some info ->
let info = Parse_info.token_location_of_info info in
spf "%s:%d:%d: FLOW %s"
info.Parse_info.file info.Parse_info.line info.Parse_info.column
(string_of_error_kind error_kind)
let (report_error : error -> unit) = fun err ->
pr2 (string_of_error err)