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
|
(*
* Vericert: Verified high-level synthesis.
* Copyright (C) 2020-2022 Yann Herklotz <yann@yannherklotz.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*)
(* [[file:../../docs/basic-block-generation.org::partition-main][partition-main]] *)
open Printf
open Clflags
open Camlcoq
open Datatypes
open Coqlib
open Maps
open AST
open Kildall
open Op
open RTLBlockInstr
open RTLBlock
(** Assuming that the nodes of the CFG [code] are numbered in reverse postorder (cf. pass
[Renumber]), an edge from [n] to [s] is a normal edge if [s < n] and a back-edge otherwise. *)
let find_edge i n =
let succ = RTL.successors_instr i in
let filt = List.filter (fun s -> P.lt n s || P.lt s (P.pred n)) succ in
((match filt with [] -> [] | _ -> [n]), filt)
let find_edges c =
PTree.fold (fun l n i ->
let f = find_edge i n in
(List.append (fst f) (fst l), List.append (snd f) (snd l))) c ([], [])
let prepend_instr i = function
| {bb_body = bb; bb_exit = e} -> {bb_body = (i :: bb); bb_exit = e}
let translate_inst = function
| RTL.Inop _ -> Some RBnop
| RTL.Iop (op, ls, dst, _) -> Some (RBop (None, op, ls, dst))
| RTL.Iload (m, addr, ls, dst, _) -> Some (RBload (None, m, addr, ls, dst))
| RTL.Istore (m, addr, ls, src, _) -> Some (RBstore (None, m, addr, ls, src))
| _ -> None
let translate_cfi = function
| RTL.Icall (s, r, ls, dst, n) -> Some (RBcall (s, r, ls, dst, n))
| RTL.Itailcall (s, r, ls) -> Some (RBtailcall (s, r, ls))
| RTL.Ibuiltin (e, ls, r, n) -> Some (RBbuiltin (e, ls, r, n))
| RTL.Icond (c, ls, dst1, dst2) -> Some (RBcond (c, ls, dst1, dst2))
| RTL.Ijumptable (r, ls) -> Some (RBjumptable (r, ls))
| RTL.Ireturn r -> Some (RBreturn r)
| _ -> None
let rec next_bblock_from_RTL is_start e (c : RTL.code) s i =
let succ = List.map (fun i -> (i, PTree.get i c)) (RTL.successors_instr i) in
let trans_inst = (translate_inst i, translate_cfi i) in
match trans_inst, succ with
| (None, Some i'), _ ->
if List.exists (fun x -> x = s) (snd e) && not is_start then
Errors.OK { bb_body = [RBnop]; bb_exit = RBgoto s }
else
Errors.OK { bb_body = [RBnop]; bb_exit = i' }
| (Some i', None), (s', Some i_n)::[] ->
if List.exists (fun x -> x = s) (fst e) then
Errors.OK { bb_body = [i']; bb_exit = RBgoto s' }
else if List.exists (fun x -> x = s) (snd e) && not is_start then
Errors.OK { bb_body = [RBnop]; bb_exit = RBgoto s }
else begin
match next_bblock_from_RTL false e c s' i_n with
| Errors.OK bb ->
Errors.OK (prepend_instr i' bb)
| Errors.Error msg -> Errors.Error msg
end
| _, _ ->
Errors.Error (Errors.msg (coqstring_of_camlstring "next_bblock_from_RTL went wrong."))
let rec traverseacc f l c =
match l with
| [] -> Errors.OK c
| x::xs ->
match f x c with
| Errors.Error msg -> Errors.Error msg
| Errors.OK x' ->
match traverseacc f xs x' with
| Errors.Error msg -> Errors.Error msg
| Errors.OK xs' -> Errors.OK xs'
let rec translate_all edge c s res =
let c_bb, translated = res in
if List.exists (fun x -> P.eq x s) translated then Errors.OK (c_bb, translated) else
(match PTree.get s c with
| None -> Errors.Error (Errors.msg (coqstring_of_camlstring "Could not translate all."))
| Some i ->
match next_bblock_from_RTL true edge c s i with
| Errors.Error msg -> Errors.Error msg
| Errors.OK {bb_body = bb; bb_exit = e} ->
let succ = List.filter (fun x -> P.lt x s) (successors_instr e) in
(match traverseacc (translate_all edge c) succ (c_bb, s :: translated) with
| Errors.Error msg -> Errors.Error msg
| Errors.OK (c', t') ->
Errors.OK (PTree.set s {bb_body = bb; bb_exit = e} c', t')))
(* Partition a function and transform it into RTLBlock. *)
let function_from_RTL f =
let e = find_edges f.RTL.fn_code in
match translate_all e f.RTL.fn_code f.RTL.fn_entrypoint (PTree.empty, []) with
| Errors.Error msg -> Errors.Error msg
| Errors.OK (c, _) ->
Errors.OK { fn_sig = f.RTL.fn_sig;
fn_stacksize = f.RTL.fn_stacksize;
fn_params = f.RTL.fn_params;
fn_entrypoint = f.RTL.fn_entrypoint;
fn_code = c
}
let partition = function_from_RTL
(* partition-main ends here *)
|