2 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
48 "\\documentclass{article}\n"^
49 "\\usepackage{fullpage}\n\n"^
50 "\\newcommand{\\U}{\\,\\mbox{\\sf{U}}\\,}\n"^
51 "\\newcommand{\\A}{\\mbox{\\sf{A}}}\n"^
52 "\\newcommand{\\E}{\\mbox{\\sf{E}}}\n"^
53 "\\newcommand{\\AX}{\\mbox{\\sf{AX}}}\n"^
54 "\\newcommand{\\EX}{\\mbox{\\sf{EX}}}\n"^
55 "\\newcommand{\\AF}{\\mbox{\\sf{AF}}}\n"^
56 "\\newcommand{\\EF}{\\mbox{\\sf{EF}}}\n"^
57 "\\newcommand{\\AG}{\\mbox{\\sf{AG}}}\n"^
58 "\\newcommand{\\EG}{\\mbox{\\sf{EG}}}\n\n"^
59 "\\newcommand{\\mita}[1]{\\mbox{\\it{{#1}}}}\n"^
60 "\\newcommand{\\mtt}[1]{\\mbox{\\tt{{#1}}}}\n"^
61 "\\newcommand{\\msf}[1]{\\mbox{\\sf{{#1}}}}\n"^
62 "\\newcommand{\\mrm}[1]{\\mbox{\\rm{{#1}}}}\n"^
63 "\\newcommand{\\mth}[1]{\\({#1}\\)}\n\n"^
64 "\\newcommand{\\ttlb}{\\mbox{\\tt \\char'173}}\n"^
65 "\\newcommand{\\ttrb}{\\mbox{\\tt \\char'175}}\n\n"^
68 let postlude = "\\end{document}"
70 let check_ct ct res
= if ct
> 60 then (res^
"\\\\\\mbox{}",0) else (res
,ct
)
72 let len = String.length s
in
77 match String.get s n
with
78 '_'
-> Printf.sprintf
"\\_%s" (loop (n
+1))
79 | '
{'
-> Printf.sprintf
"{\\ttlb}%s" (loop (n
+1))
80 | '
}'
-> Printf.sprintf
"{\\ttrb}%s" (loop (n
+1))
81 | '
>'
-> Printf.sprintf
"\\mth{>}%s" (loop (n
+1))
82 | c
-> Printf.sprintf
"%c%s" c
(loop (n
+1)) in
83 (Printf.sprintf
"\\mita{%s}" (loop 0),len)
85 let modif2c pv
= function
86 CTL.Modif
(v
) -> let (s
,n
) = texify(pv v
) in (Printf.sprintf
"_{%s}" s
,n
)
87 | CTL.UnModif
(v
) -> let (s
,n
) = texify(pv v
) in (Printf.sprintf
"_{%s}" s
,n
)
88 | CTL.Control
-> ("",0)
90 let print_diamond ct
= function
91 CTL.FORWARD
-> ("",ct
)
92 | CTL.BACKWARD
-> ("\\Delta",ct
+1)
94 let rec ctl2c ct pp pv
= function
95 CTL.False
-> ("\\msf{false}",5)
96 | CTL.True
-> ("\\msf{true}",4)
99 let (resv
,n1
) = modif2c pv v
in
102 let (res
,ct
) = wrap
(ct
+1) pp pv f
in
104 | CTL.Exists
(_
,v
,f
) ->
105 let (res1
,len) = texify(pv v
) in
107 let (res1
,ct) = check_ct ct res1
in
108 let (res2
,ct) = existswrap
(ct+1) pp pv f
in
109 ("\\exists "^res1^
" . "^res2
,ct)
110 | CTL.And
(_
,f1
,f2
) ->
111 let (res1
,ct) = andwrap
ct pp pv f1
in
112 let (res1
,ct) = check_ct ct res1
in
113 let (res2
,ct) = andwrap
(ct+1) pp pv f2
in
114 (res1^
" \\wedge "^res2
,ct)
115 | CTL.AndAny
(dir
,_
,f1
,f2
) ->
116 let (diamond
,ct) = print_diamond (ct+2) dir
in
117 let (res1
,ct) = andwrap
ct pp pv f1
in
118 let (res1
,ct) = check_ct ct res1
in
119 let (res2
,ct) = andwrap
(ct+1) pp pv f2
in
120 (res1^
" \\wedge? "^diamond^res2
,ct)
121 | CTL.HackForStmt
(dir
,_
,f1
,f2
) ->
122 let (diamond
,ct) = print_diamond (ct+2) dir
in
123 let (res1
,ct) = andwrap
ct pp pv f1
in
124 let (res1
,ct) = check_ct ct res1
in
125 let (res2
,ct) = andwrap
(ct+1) pp pv f2
in
126 (res1^
" \\wedge{h} "^diamond^res2
,ct)
128 let (res1
,ct) = orwrap
ct pp pv f1
in
129 let (res1
,ct) = check_ct ct res1
in
130 let (res2
,ct) = orwrap
(ct+1) pp pv f2
in
131 (res1^
" \\vee "^res2
,ct)
132 | CTL.SeqOr
(f1
,f2
) ->
133 let (res1
,ct) = orwrap
ct pp pv f1
in
134 let (res1
,ct) = check_ct ct res1
in
135 let (res2
,ct) = orwrap
(ct+1) pp pv f2
in
136 (res1^
" \\mid "^res2
,ct)
137 | CTL.Implies
(f1
,f2
) ->
138 let (res1
,ct) = wrap
ct pp pv f1
in
139 let (res1
,ct) = check_ct ct res1
in
140 let (res2
,ct) = wrap
(ct+1) pp pv f2
in
141 (res1^
" \\rightarrow "^res2
,ct)
143 let (diamond
,ct) = print_diamond (ct+2) dir
in
144 let (res
,ct) = pathwrap
ct pp pv f
145 in ("\\AF"^diamond^res
,ct)
147 let (diamond
,ct) = print_diamond (ct+2) dir
in
148 let (res
,ct) = pathwrap
ct pp pv f
149 in ("\\AX"^diamond^res
,ct)
151 let (diamond
,ct) = print_diamond (ct+2) dir
in
152 let (res
,ct) = pathwrap
ct pp pv f
153 in ("\\AG"^diamond^res
,ct)
154 | CTL.AW
(dir
,_
,f1
,f2
) ->
155 let (diamond
,ct) = print_diamond (ct+1) dir
in
156 let (res1
,ct) = existswrap
(ct+1) pp pv f1
in
157 let (res1
,ct) = check_ct ct res1
in
158 let (res2
,ct) = existswrap
(ct+3) pp pv f2
in
159 ("\\"^diamond^
"A["^res1^
" W "^res2^
"]\n",ct)
160 | CTL.AU
(dir
,_
,f1
,f2
) ->
161 let (diamond
,ct) = print_diamond (ct+1) dir
in
162 let (res1
,ct) = existswrap
(ct+1) pp pv f1
in
163 let (res1
,ct) = check_ct ct res1
in
164 let (res2
,ct) = existswrap
(ct+3) pp pv f2
in
165 ("\\"^diamond^
"A["^res1^
" \\U "^res2^
"]\n",ct)
167 let (diamond
,ct) = print_diamond (ct+2) dir
in
168 let (res
,ct) = pathwrap
ct pp pv f
169 in ("\\EF"^diamond^res
,ct)
171 let (diamond
,ct) = print_diamond (ct+2) dir
in
172 let (res
,ct) = pathwrap
ct pp pv f
173 in ("\\EX"^diamond^res
,ct)
175 let (diamond
,ct) = print_diamond (ct+2) dir
in
176 let (res
,ct) = pathwrap
ct pp pv f
177 in ("\\EG"^diamond^res
,ct)
178 | CTL.EU
(dir
,f1
,f2
) ->
179 let (diamond
,ct) = print_diamond (ct+1) dir
in
180 let (res1
,ct) = existswrap
(ct+1) pp pv f1
in
181 let (res1
,ct) = check_ct ct res1
in
182 let (res2
,ct) = existswrap
(ct+3) pp pv f2
in
183 ("\\E"^diamond^
"["^res1^
" \\U "^res2^
"]\n",ct)
185 let (v
,len) = texify(pv
(make_var v
)) in (v
,len+ct)
186 | CTL.Let
(v
,f1
,f2
) ->
187 let (v
,len) = texify (pv
(make_var v
)) in
188 let (res1
,ct) = letwrap
(ct+len+5) pp pv f1
in
189 let (res1
,ct) = check_ct ct res1
in
190 let (res2
,ct) = letwrap
(ct+3) pp pv f2
in
191 let (res2
,ct) = check_ct ct res2
in
193 "\\mita{\\sf{let}} \\, %s = %s \\, \\mita{\\sf{in}} \\, %s\n"
195 | CTL.LetR
(d
,v
,f1
,f2
) ->
196 let (diamond
,ct) = print_diamond (ct+2) d
in
197 let (v
,len) = texify (pv
(make_var v
)) in
198 let (res1
,ct) = letwrap
(ct+len+5) pp pv f1
in
199 let (res1
,ct) = check_ct ct res1
in
200 let (res2
,ct) = letwrap
(ct+3) pp pv f2
in
201 let (res2
,ct) = check_ct ct res2
in
203 "\\mita{\\sf{let}}%s \\, %s = %s \\, \\mita{\\sf{in}} \\, %s\n"
204 diamond v res1 res2
, ct)
206 let (res
,ct) = pathwrap
ct pp pv f
209 let (res
,ct) = pathwrap
ct pp pv f
210 in ("("^res^
")^{innerAnd}",ct+10)
211 | CTL.XX
(_
) -> failwith
"should not be printed"
213 and make_var x
= ("",x
)
215 and wrap
ct pp pv x
=
217 CTL.Ref _
| CTL.False
| CTL.True
| CTL.Pred
(_
) -> ctl2c ct pp pv x
219 let (res
,ct) = ctl2c (ct+1) pp pv x
in
220 (Printf.sprintf
"(%s)" res
,ct+1)
222 and andwrap
ct pp pv x
=
224 CTL.Ref _
| CTL.And
(_
,_
,_
) | CTL.False
| CTL.True
| CTL.Pred
(_
) ->
227 let (res
,ct) = ctl2c (ct+1) pp pv x
in
228 (Printf.sprintf
"(%s)" res
,ct+1)
230 and orwrap
ct pp pv x
=
232 CTL.Ref _
| CTL.Or
(_
,_
) | CTL.False
| CTL.True
| CTL.Pred
(_
) ->
235 let (res
,ct) = ctl2c (ct+1) pp pv x
in
236 (Printf.sprintf
"(%s)" res
,ct+1)
238 and pathwrap
ct pp pv x
=
240 CTL.Ref _
| CTL.AX
(_
,_
,_
) | CTL.AF
(_
,_
,_
) | CTL.AG
(_
,_
,_
) | CTL.AU
(_
,_
,_
,_
)
241 | CTL.EX
(_
,_
) | CTL.EF
(_
,_
) | CTL.EG
(_
,_
) | CTL.EU
(_
,_
,_
) ->
244 let (res
,ct) = ctl2c (ct+1) pp pv x
in
245 (Printf.sprintf
"(%s)" res
,ct+1)
247 and existswrap
ct pp pv x
=
249 CTL.Ref _
| CTL.AX
(_
,_
,_
) | CTL.AF
(_
,_
,_
) | CTL.AG
(_
,_
,_
) | CTL.AU
(_
,_
,_
,_
)
251 | CTL.EX
(_
,_
) | CTL.EF
(_
,_
) | CTL.EG
(_
,_
) | CTL.EU
(_
,_
,_
) | CTL.Exists
(_
,_
,_
)
252 | CTL.True
| CTL.False
| CTL.Not
(_
) ->
255 let (res
,ct) = ctl2c (ct+1) pp pv x
in
256 (Printf.sprintf
"(%s)" res
,ct+1)
258 and letwrap
ct pp pv x
=
261 let (res
,ct) = ctl2c (ct+1) pp pv x
in (Printf.sprintf
"(%s)" res
,ct+1)
262 | _
-> ctl2c ct pp pv x
264 let ctltotex rule pp pv ctls o
=
265 Printf.fprintf o
"\\begin{quote}\\begin{verbatim}\n";
266 Printf.fprintf o
"%s\n" (Pretty_print_cocci.unparse_to_string rule
);
267 Printf.fprintf o
"\\end{verbatim}\\end{quote}\n\n";
270 Printf.fprintf o
"\\[\\begin{array}{l}\n";
271 let (res
,_
) = ctl2c 0 pp pv ctl
in
272 Printf.fprintf o
"%s\n" res
)
274 Printf.fprintf o
"\\end{array}\\]\n\n"
276 let make_prelude o
= Printf.fprintf o
"%s\n" prelude
277 let make_postlude o
= Printf.fprintf o
"%s\n" postlude
279 (* ----------------------------------------------------------------------- *)
283 let pred2c = function
284 Lib_engine.InLoop
-> ("\\msf{InLoop}",6)
285 | Lib_engine.TrueBranch
-> ("\\msf{TrueBranch}",10)
286 | Lib_engine.FalseBranch
-> ("\\msf{FalseBranch}",11)
287 | Lib_engine.After
-> ("\\msf{After}",5)
288 | Lib_engine.FallThrough
-> ("\\msf{FallThrough}",11)
289 | Lib_engine.LoopFallThrough
-> ("\\msf{LoopFallThrough}",15)
290 | Lib_engine.Return
-> ("\\msf{Return}",6)
291 | Lib_engine.FunHeader
-> ("\\msf{FunHeader}",9)
292 | Lib_engine.Top
-> ("\\msf{Top}",3)
293 | Lib_engine.Exit
-> ("\\msf{Exit}",4)
294 | Lib_engine.ErrorExit
-> ("\\msf{ErrorExit}",9)
295 | Lib_engine.Paren
(s
) ->
297 ("\\msf{Paren}("^
s^
")",7+(String.length
s))
298 | Lib_engine.Label
(s) ->
300 ("\\msf{Label}("^
s^
")",7+(String.length
s))
301 | Lib_engine.BCLabel
(s) ->
303 ("\\msf{BreakContinueLabel}("^
s^
")",20+(String.length
s))
304 | Lib_engine.PrefixLabel
(s) ->
306 ("\\msf{PrefixLabel}("^
s^
")",13+(String.length
s))
307 | Lib_engine.Match
(re
) ->
308 let s = Pretty_print_cocci.rule_elem_to_string re
in
309 let (s,len) = texify s in
310 (Printf.sprintf
"%s" s,len)
311 | Lib_engine.BindGood
(nm
) ->
313 ("\\msf{Good}("^
s^
")",6+(String.length
s))
314 | Lib_engine.BindBad
(nm
) ->
316 ("\\msf{Bad}("^
s^
")",5+(String.length
s))
317 | Lib_engine.Goto
-> ("goto",4)
318 | Lib_engine.FakeBrace
-> ("fake\\_brace",10)
320 let totex out_file rules ctls
=
321 let o = open_out out_file
in
324 (function ast_list
->
326 let (ctls
,_
) = List.split ctls
in
327 ctltotex ast_list
pred2c (function (_
,x
) -> x
) ctls
o)