2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
31 "\\documentclass{article}\n"^
32 "\\usepackage{fullpage}\n\n"^
33 "\\newcommand{\\U}{\\,\\mbox{\\sf{U}}\\,}\n"^
34 "\\newcommand{\\A}{\\mbox{\\sf{A}}}\n"^
35 "\\newcommand{\\E}{\\mbox{\\sf{E}}}\n"^
36 "\\newcommand{\\AX}{\\mbox{\\sf{AX}}}\n"^
37 "\\newcommand{\\EX}{\\mbox{\\sf{EX}}}\n"^
38 "\\newcommand{\\AF}{\\mbox{\\sf{AF}}}\n"^
39 "\\newcommand{\\EF}{\\mbox{\\sf{EF}}}\n"^
40 "\\newcommand{\\AG}{\\mbox{\\sf{AG}}}\n"^
41 "\\newcommand{\\EG}{\\mbox{\\sf{EG}}}\n\n"^
42 "\\newcommand{\\mita}[1]{\\mbox{\\it{{#1}}}}\n"^
43 "\\newcommand{\\mtt}[1]{\\mbox{\\tt{{#1}}}}\n"^
44 "\\newcommand{\\msf}[1]{\\mbox{\\sf{{#1}}}}\n"^
45 "\\newcommand{\\mrm}[1]{\\mbox{\\rm{{#1}}}}\n"^
46 "\\newcommand{\\mth}[1]{\\({#1}\\)}\n\n"^
47 "\\newcommand{\\ttlb}{\\mbox{\\tt \\char'173}}\n"^
48 "\\newcommand{\\ttrb}{\\mbox{\\tt \\char'175}}\n\n"^
51 let postlude = "\\end{document}"
53 let check_ct ct res
= if ct
> 60 then (res^
"\\\\\\mbox{}",0) else (res
,ct
)
55 let len = String.length s
in
60 match String.get s n
with
61 '_'
-> Printf.sprintf
"\\_%s" (loop (n
+1))
62 | '
{'
-> Printf.sprintf
"{\\ttlb}%s" (loop (n
+1))
63 | '
}'
-> Printf.sprintf
"{\\ttrb}%s" (loop (n
+1))
64 | '
>'
-> Printf.sprintf
"\\mth{>}%s" (loop (n
+1))
65 | c
-> Printf.sprintf
"%c%s" c
(loop (n
+1)) in
66 (Printf.sprintf
"\\mita{%s}" (loop 0),len)
68 let modif2c pv
= function
69 CTL.Modif
(v
) -> let (s
,n
) = texify(pv v
) in (Printf.sprintf
"_{%s}" s
,n
)
70 | CTL.UnModif
(v
) -> let (s
,n
) = texify(pv v
) in (Printf.sprintf
"_{%s}" s
,n
)
71 | CTL.Control
-> ("",0)
73 let print_diamond ct
= function
74 CTL.FORWARD
-> ("",ct
)
75 | CTL.BACKWARD
-> ("\\Delta",ct
+1)
77 let rec ctl2c ct pp pv
= function
78 CTL.False
-> ("\\msf{false}",5)
79 | CTL.True
-> ("\\msf{true}",4)
82 let (resv
,n1
) = modif2c pv v
in
85 let (res
,ct
) = wrap
(ct
+1) pp pv f
in
87 | CTL.Exists
(_
,v
,f
) ->
88 let (res1
,len) = texify(pv v
) in
90 let (res1
,ct) = check_ct ct res1
in
91 let (res2
,ct) = existswrap
(ct+1) pp pv f
in
92 ("\\exists "^res1^
" . "^res2
,ct)
94 let (res1
,ct) = andwrap
ct pp pv f1
in
95 let (res1
,ct) = check_ct ct res1
in
96 let (res2
,ct) = andwrap
(ct+1) pp pv f2
in
97 (res1^
" \\wedge "^res2
,ct)
98 | CTL.AndAny
(dir
,_
,f1
,f2
) ->
99 let (diamond
,ct) = print_diamond (ct+2) dir
in
100 let (res1
,ct) = andwrap
ct pp pv f1
in
101 let (res1
,ct) = check_ct ct res1
in
102 let (res2
,ct) = andwrap
(ct+1) pp pv f2
in
103 (res1^
" \\wedge? "^diamond^res2
,ct)
104 | CTL.HackForStmt
(dir
,_
,f1
,f2
) ->
105 let (diamond
,ct) = print_diamond (ct+2) dir
in
106 let (res1
,ct) = andwrap
ct pp pv f1
in
107 let (res1
,ct) = check_ct ct res1
in
108 let (res2
,ct) = andwrap
(ct+1) pp pv f2
in
109 (res1^
" \\wedge{h} "^diamond^res2
,ct)
111 let (res1
,ct) = orwrap
ct pp pv f1
in
112 let (res1
,ct) = check_ct ct res1
in
113 let (res2
,ct) = orwrap
(ct+1) pp pv f2
in
114 (res1^
" \\vee "^res2
,ct)
115 | CTL.SeqOr
(f1
,f2
) ->
116 let (res1
,ct) = orwrap
ct pp pv f1
in
117 let (res1
,ct) = check_ct ct res1
in
118 let (res2
,ct) = orwrap
(ct+1) pp pv f2
in
119 (res1^
" \\mid "^res2
,ct)
120 | CTL.Implies
(f1
,f2
) ->
121 let (res1
,ct) = wrap
ct pp pv f1
in
122 let (res1
,ct) = check_ct ct res1
in
123 let (res2
,ct) = wrap
(ct+1) pp pv f2
in
124 (res1^
" \\rightarrow "^res2
,ct)
126 let (diamond
,ct) = print_diamond (ct+2) dir
in
127 let (res
,ct) = pathwrap
ct pp pv f
128 in ("\\AF"^diamond^res
,ct)
130 let (diamond
,ct) = print_diamond (ct+2) dir
in
131 let (res
,ct) = pathwrap
ct pp pv f
132 in ("\\AX"^diamond^res
,ct)
134 let (diamond
,ct) = print_diamond (ct+2) dir
in
135 let (res
,ct) = pathwrap
ct pp pv f
136 in ("\\AG"^diamond^res
,ct)
137 | CTL.AW
(dir
,_
,f1
,f2
) ->
138 let (diamond
,ct) = print_diamond (ct+1) dir
in
139 let (res1
,ct) = existswrap
(ct+1) pp pv f1
in
140 let (res1
,ct) = check_ct ct res1
in
141 let (res2
,ct) = existswrap
(ct+3) pp pv f2
in
142 ("\\"^diamond^
"A["^res1^
" W "^res2^
"]\n",ct)
143 | CTL.AU
(dir
,_
,f1
,f2
) ->
144 let (diamond
,ct) = print_diamond (ct+1) dir
in
145 let (res1
,ct) = existswrap
(ct+1) pp pv f1
in
146 let (res1
,ct) = check_ct ct res1
in
147 let (res2
,ct) = existswrap
(ct+3) pp pv f2
in
148 ("\\"^diamond^
"A["^res1^
" \\U "^res2^
"]\n",ct)
150 let (diamond
,ct) = print_diamond (ct+2) dir
in
151 let (res
,ct) = pathwrap
ct pp pv f
152 in ("\\EF"^diamond^res
,ct)
154 let (diamond
,ct) = print_diamond (ct+2) dir
in
155 let (res
,ct) = pathwrap
ct pp pv f
156 in ("\\EX"^diamond^res
,ct)
158 let (diamond
,ct) = print_diamond (ct+2) dir
in
159 let (res
,ct) = pathwrap
ct pp pv f
160 in ("\\EG"^diamond^res
,ct)
161 | CTL.EU
(dir
,f1
,f2
) ->
162 let (diamond
,ct) = print_diamond (ct+1) dir
in
163 let (res1
,ct) = existswrap
(ct+1) pp pv f1
in
164 let (res1
,ct) = check_ct ct res1
in
165 let (res2
,ct) = existswrap
(ct+3) pp pv f2
in
166 ("\\E"^diamond^
"["^res1^
" \\U "^res2^
"]\n",ct)
168 let (v
,len) = texify(pv
(make_var v
)) in (v
,len+ct)
169 | CTL.Let
(v
,f1
,f2
) ->
170 let (v
,len) = texify (pv
(make_var v
)) in
171 let (res1
,ct) = letwrap
(ct+len+5) pp pv f1
in
172 let (res1
,ct) = check_ct ct res1
in
173 let (res2
,ct) = letwrap
(ct+3) pp pv f2
in
174 let (res2
,ct) = check_ct ct res2
in
176 "\\mita{\\sf{let}} \\, %s = %s \\, \\mita{\\sf{in}} \\, %s\n"
178 | CTL.LetR
(d
,v
,f1
,f2
) ->
179 let (diamond
,ct) = print_diamond (ct+2) d
in
180 let (v
,len) = texify (pv
(make_var v
)) in
181 let (res1
,ct) = letwrap
(ct+len+5) pp pv f1
in
182 let (res1
,ct) = check_ct ct res1
in
183 let (res2
,ct) = letwrap
(ct+3) pp pv f2
in
184 let (res2
,ct) = check_ct ct res2
in
186 "\\mita{\\sf{let}}%s \\, %s = %s \\, \\mita{\\sf{in}} \\, %s\n"
187 diamond v res1 res2
, ct)
189 let (res
,ct) = pathwrap
ct pp pv f
192 let (res
,ct) = pathwrap
ct pp pv f
193 in ("("^res^
")^{innerAnd}",ct+10)
194 | CTL.XX
(_
) -> failwith
"should not be printed"
196 and make_var x
= ("",x
)
198 and wrap
ct pp pv x
=
200 CTL.Ref _
| CTL.False
| CTL.True
| CTL.Pred
(_
) -> ctl2c ct pp pv x
202 let (res
,ct) = ctl2c (ct+1) pp pv x
in
203 (Printf.sprintf
"(%s)" res
,ct+1)
205 and andwrap
ct pp pv x
=
207 CTL.Ref _
| CTL.And
(_
,_
,_
) | CTL.False
| CTL.True
| CTL.Pred
(_
) ->
210 let (res
,ct) = ctl2c (ct+1) pp pv x
in
211 (Printf.sprintf
"(%s)" res
,ct+1)
213 and orwrap
ct pp pv x
=
215 CTL.Ref _
| CTL.Or
(_
,_
) | CTL.False
| CTL.True
| CTL.Pred
(_
) ->
218 let (res
,ct) = ctl2c (ct+1) pp pv x
in
219 (Printf.sprintf
"(%s)" res
,ct+1)
221 and pathwrap
ct pp pv x
=
223 CTL.Ref _
| CTL.AX
(_
,_
,_
) | CTL.AF
(_
,_
,_
) | CTL.AG
(_
,_
,_
) | CTL.AU
(_
,_
,_
,_
)
224 | CTL.EX
(_
,_
) | CTL.EF
(_
,_
) | CTL.EG
(_
,_
) | CTL.EU
(_
,_
,_
) ->
227 let (res
,ct) = ctl2c (ct+1) pp pv x
in
228 (Printf.sprintf
"(%s)" res
,ct+1)
230 and existswrap
ct pp pv x
=
232 CTL.Ref _
| CTL.AX
(_
,_
,_
) | CTL.AF
(_
,_
,_
) | CTL.AG
(_
,_
,_
) | CTL.AU
(_
,_
,_
,_
)
234 | CTL.EX
(_
,_
) | CTL.EF
(_
,_
) | CTL.EG
(_
,_
) | CTL.EU
(_
,_
,_
) | CTL.Exists
(_
,_
,_
)
235 | CTL.True
| CTL.False
| CTL.Not
(_
) ->
238 let (res
,ct) = ctl2c (ct+1) pp pv x
in
239 (Printf.sprintf
"(%s)" res
,ct+1)
241 and letwrap
ct pp pv x
=
244 let (res
,ct) = ctl2c (ct+1) pp pv x
in (Printf.sprintf
"(%s)" res
,ct+1)
245 | _
-> ctl2c ct pp pv x
247 let ctltotex rule pp pv ctls o
=
248 Printf.fprintf o
"\\begin{quote}\\begin{verbatim}\n";
249 Printf.fprintf o
"%s\n" (Pretty_print_cocci.unparse_to_string rule
);
250 Printf.fprintf o
"\\end{verbatim}\\end{quote}\n\n";
253 Printf.fprintf o
"\\[\\begin{array}{l}\n";
254 let (res
,_
) = ctl2c 0 pp pv ctl
in
255 Printf.fprintf o
"%s\n" res
)
257 Printf.fprintf o
"\\end{array}\\]\n\n"
259 let make_prelude o
= Printf.fprintf o
"%s\n" prelude
260 let make_postlude o
= Printf.fprintf o
"%s\n" postlude
262 (* ----------------------------------------------------------------------- *)
266 let pred2c = function
267 Lib_engine.InLoop
-> ("\\msf{InLoop}",6)
268 | Lib_engine.TrueBranch
-> ("\\msf{TrueBranch}",10)
269 | Lib_engine.FalseBranch
-> ("\\msf{FalseBranch}",11)
270 | Lib_engine.After
-> ("\\msf{After}",5)
271 | Lib_engine.FallThrough
-> ("\\msf{FallThrough}",11)
272 | Lib_engine.LoopFallThrough
-> ("\\msf{LoopFallThrough}",15)
273 | Lib_engine.Return
-> ("\\msf{Return}",6)
274 | Lib_engine.FunHeader
-> ("\\msf{FunHeader}",9)
275 | Lib_engine.UnsafeBrace
-> ("\\msf{UnsafeBrace}",11)
276 | Lib_engine.Top
-> ("\\msf{Top}",3)
277 | Lib_engine.Exit
-> ("\\msf{Exit}",4)
278 | Lib_engine.ErrorExit
-> ("\\msf{ErrorExit}",9)
279 | Lib_engine.Paren
(s
) ->
281 ("\\msf{Paren}("^
s^
")",7+(String.length
s))
282 | Lib_engine.Label
(s) ->
284 ("\\msf{Label}("^
s^
")",7+(String.length
s))
285 | Lib_engine.BCLabel
(s) ->
287 ("\\msf{BreakContinueLabel}("^
s^
")",20+(String.length
s))
288 | Lib_engine.PrefixLabel
(s) ->
290 ("\\msf{PrefixLabel}("^
s^
")",13+(String.length
s))
291 | Lib_engine.Match
(re
) ->
292 let s = Pretty_print_cocci.rule_elem_to_string re
in
293 let (s,len) = texify s in
294 (Printf.sprintf
"%s" s,len)
295 | Lib_engine.BindGood
(nm
) ->
297 ("\\msf{Good}("^
s^
")",6+(String.length
s))
298 | Lib_engine.BindBad
(nm
) ->
300 ("\\msf{Bad}("^
s^
")",5+(String.length
s))
301 | Lib_engine.Goto
-> ("goto",4)
302 | Lib_engine.FakeBrace
-> ("fake\\_brace",10)
304 let totex out_file rules ctls
=
305 let o = open_out out_file
in
308 (function ast_list
->
310 let (ctls
,_
) = List.split ctls
in
311 ctltotex ast_list
pred2c (function (_
,x
) -> x
) ctls
o)