summaryrefslogtreecommitdiff
path: root/cil.patch/cil.ml.patch
blob: a49b73d1a559741144d2fefeced5e0ea65f1dbb0 (plain)
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
*** ../cil/src/cil.ml	2006-05-21 06:14:15.000000000 +0200
--- ../cil_patch/src/cil.ml	2006-07-25 10:57:30.686790845 +0200
***************
*** 1,3 ****
--- 1,6 ----
+ (* MODIF: Loop constructor replaced by 3 constructors: While, DoWhile, For. *)
+ (* MODIF: useLogicalOperators flag set to true by default. *)
+ 
  (*
   *
   * Copyright (c) 2001-2003,
***************
*** 63,69 ****
                                         * print output for the MS VC 
                                         * compiler. Default is GCC *)
  
! let useLogicalOperators = ref false
  
  
  module M = Machdep
--- 66,72 ----
                                         * print output for the MS VC 
                                         * compiler. Default is GCC *)
  
! let useLogicalOperators = ref (*false*) true
  
  
  module M = Machdep
***************
*** 684,692 ****
    | Goto of stmt ref * location         (** A goto statement. Appears from 
                                              actual goto's in the code. *)
    | Break of location                   (** A break to the end of the nearest 
!                                              enclosing Loop or Switch *)
    | Continue of location                (** A continue to the start of the 
!                                             nearest enclosing [Loop] *)
    | If of exp * block * block * location (** A conditional. 
                                               Two successors, the "then" and 
                                               the "else" branches. Both 
--- 687,695 ----
    | Goto of stmt ref * location         (** A goto statement. Appears from 
                                              actual goto's in the code. *)
    | Break of location                   (** A break to the end of the nearest 
!                                              enclosing loop or Switch *)
    | Continue of location                (** A continue to the start of the 
!                                             nearest enclosing loop *)
    | If of exp * block * block * location (** A conditional. 
                                               Two successors, the "then" and 
                                               the "else" branches. Both 
***************
*** 701,706 ****
--- 704,710 ----
                                             you can get from the labels of the 
                                             statement *)
  
+ (*
    | Loop of block * location * (stmt option) * (stmt option) 
                                             (** A [while(1)] loop. The 
                                              * termination test is implemented 
***************
*** 713,718 ****
--- 717,726 ----
                                              * and the second will point to 
                                              * the stmt containing the break 
                                              * label for this loop. *)
+ *)
+   | While of exp * block * location                 (** A while loop. *)
+   | DoWhile of exp * block * location               (** A do...while loop. *)
+   | For of block * exp * block * block * location   (** A for loop. *)
  
    | Block of block                      (** Just a block of statements. Use it 
                                              as a way to keep some attributes 
***************
*** 1040,1046 ****
--- 1048,1059 ----
      | Continue(loc) -> loc
      | If(_, _, _, loc) -> loc
      | Switch (_, _, _, loc) -> loc
+ (*
      | Loop (_, loc, _, _) -> loc
+ *)
+     | While (_, _, loc) -> loc
+     | DoWhile (_, _, loc) -> loc
+     | For (_, _, _, _, loc) -> loc
      | Block b -> if b.bstmts == [] then lu 
                   else get_stmtLoc ((List.hd b.bstmts).skind)
      | TryFinally (_, _, l) -> l
***************
*** 1524,1533 ****
--- 1537,1549 ----
  
  let mkWhile ~(guard:exp) ~(body: stmt list) : stmt list = 
    (* Do it like this so that the pretty printer recognizes it *)
+ (*
    [ mkStmt (Loop (mkBlock (mkStmt (If(guard, 
                                        mkBlock [ mkEmptyStmt () ], 
                                        mkBlock [ mkStmt (Break lu)], lu)) ::
                             body), lu, None, None)) ]
+ *)
+   [ mkStmt (While (guard, mkBlock body, lu)) ]
  
  
  
***************
*** 3448,3453 ****
--- 3464,3471 ----
                  ++ self#pExp () e
                  ++ text ") "
                  ++ self#pBlock () b)
+ 
+ (*
      | Loop(b, l, _, _) -> begin
          (* Maybe the first thing is a conditional. Turn it into a WHILE *)
          try
***************
*** 3484,3489 ****
--- 3502,3540 ----
                    ++ text "ile (1) "
                    ++ self#pBlock () b)
      end
+ *)
+ 
+     | While (e, b, l) ->
+         self#pLineDirective l
+           ++ (align
+                 ++ text "while ("
+                 ++ self#pExp () e
+                 ++ text ") "
+                 ++ self#pBlock () b)
+ 
+     | DoWhile (e, b, l) ->
+         self#pLineDirective l
+           ++ (align
+                 ++ text "do "
+                 ++ self#pBlock () b
+                 ++ text " while ("
+                 ++ self#pExp () e
+                 ++ text ");")
+ 
+     | For (bInit, e, bIter, b, l) ->
+ 	ignore (E.warn
+ 		  "in for loops, the 1st and 3rd expressions are not printed");
+           self#pLineDirective l
+             ++ (align
+                   ++ text "for ("
+                   ++ text "/* ??? */"   (* self#pBlock () bInit *)
+                   ++ text "; "
+                   ++ self#pExp () e
+                   ++ text "; "
+                   ++ text "/* ??? */"   (* self#pBlock() bIter *)
+                   ++ text ") "
+                   ++ self#pBlock () b)
+ 
      | Block b -> align ++ self#pBlock () b
        
      | TryFinally (b, h, l) -> 
***************
*** 4705,4713 ****
--- 4756,4781 ----
      | Return (Some e, l) -> 
          let e' = fExp e in
          if e' != e then Return (Some e', l) else s.skind
+ (*
      | Loop (b, l, s1, s2) -> 
          let b' = fBlock b in
          if b' != b then Loop (b', l, s1, s2) else s.skind
+ *)
+     | While (e, b, l) ->
+ 	let e' = fExp e in 
+         let b' = fBlock b in
+           if e' != e || b' != b then While (e', b', l) else s.skind
+     | DoWhile (e, b, l) ->
+ 	let b' = fBlock b in
+         let e' = fExp e in 
+           if e' != e || b' != b then DoWhile (e', b', l) else s.skind
+     | For (bInit, e, bIter, b, l) -> 
+ 	let bInit' = fBlock bInit in
+ 	let e' = fExp e in
+ 	let bIter' = fBlock bIter in
+ 	let b' = fBlock b in
+ 	  if bInit' != bInit || e' != e || bIter' != bIter || b' != b then
+ 	    For (bInit', e', bIter', b', l) else s.skind
      | If(e, s1, s2, l) -> 
          let e' = fExp e in 
          (*if e queued any instructions, pop them here and remember them so that
***************
*** 5180,5186 ****
--- 5248,5262 ----
            peepHole1 doone tb.bstmts;
            peepHole1 doone eb.bstmts
        | Switch (e, b, _, _) -> peepHole1 doone b.bstmts
+ (*
        | Loop (b, l, _, _) -> peepHole1 doone b.bstmts
+ *)
+       | While (_, b, _) -> peepHole1 doone b.bstmts
+       | DoWhile (_, b, _) -> peepHole1 doone b.bstmts
+       | For (bInit, _, bIter, b, _) ->
+ 	  peepHole1 doone bInit.bstmts;
+ 	  peepHole1 doone bIter.bstmts;
+ 	  peepHole1 doone b.bstmts
        | Block b -> peepHole1 doone b.bstmts
        | TryFinally (b, h, l) -> 
            peepHole1 doone b.bstmts; 
***************
*** 5214,5220 ****
--- 5290,5304 ----
            peepHole2 dotwo tb.bstmts;
            peepHole2 dotwo eb.bstmts
        | Switch (e, b, _, _) -> peepHole2 dotwo b.bstmts
+ (*
        | Loop (b, l, _, _) -> peepHole2 dotwo b.bstmts
+ *)
+       | While (_, b, _) -> peepHole2 dotwo b.bstmts
+       | DoWhile (_, b, _) -> peepHole2 dotwo b.bstmts
+       | For (bInit, _, bIter, b, _) ->
+ 	  peepHole2 dotwo bInit.bstmts;
+ 	  peepHole2 dotwo bIter.bstmts;
+ 	  peepHole2 dotwo b.bstmts
        | Block b -> peepHole2 dotwo b.bstmts
        | TryFinally (b, h, l) -> peepHole2 dotwo b.bstmts; 
                                  peepHole2 dotwo h.bstmts
***************
*** 5887,5892 ****
--- 5971,5977 ----
          [] -> trylink s fallthrough
        | hd :: tl -> (link s hd ; succpred_block b2 fallthrough ))
  
+ (*
    | Loop(b,l,_,_) -> 
        begin match b.bstmts with
          [] -> failwith "computeCFGInfo: empty loop" 
***************
*** 5894,5899 ****
--- 5979,6011 ----
            link s hd ; 
            succpred_block b (Some(hd))
        end
+ *)
+ 
+   | While (e, b, l) -> begin match b.bstmts with
+                        | [] -> failwith "computeCFGInfo: empty loop" 
+ 	               | hd :: tl -> link s hd ;
+                                      succpred_block b (Some(hd))
+                        end
+ 
+   | DoWhile (e, b, l) ->begin match b.bstmts with
+                        |  [] -> failwith "computeCFGInfo: empty loop" 
+ 	               | hd :: tl -> link s hd ;
+                                      succpred_block b (Some(hd))
+                        end
+ 
+   | For (bInit, e, bIter, b, l) ->
+       (match bInit.bstmts with
+          |  [] -> failwith "computeCFGInfo: empty loop" 
+ 	 | hd :: tl -> link s hd ;
+              succpred_block bInit (Some(hd))) ;
+       (match bIter.bstmts with
+          |  [] -> failwith "computeCFGInfo: empty loop" 
+ 	 | hd :: tl -> link s hd ;
+              succpred_block bIter (Some(hd))) ;
+       (match b.bstmts with
+          |  [] -> failwith "computeCFGInfo: empty loop" 
+ 	 | hd :: tl -> link s hd ;
+              succpred_block b (Some(hd))) ;
  
    | Block(b) -> begin match b.bstmts with
                    [] -> trylink s fallthrough
***************
*** 5985,5991 ****
        let i = get_switch_count () in 
        let break_stmt = mkStmt (Instr []) in
        break_stmt.labels <- 
! 				[Label((Printf.sprintf "switch_%d_break" i),l,false)] ;
        let break_block = mkBlock [ break_stmt ] in
        let body_block = b in 
        let body_if_stmtkind = (If(zero,body_block,break_block,l)) in
--- 6097,6103 ----
        let i = get_switch_count () in 
        let break_stmt = mkStmt (Instr []) in
        break_stmt.labels <- 
! 	[Label((Printf.sprintf "switch_%d_break" i),l,false)] ;
        let break_block = mkBlock [ break_stmt ] in
        let body_block = b in 
        let body_if_stmtkind = (If(zero,body_block,break_block,l)) in
***************
*** 6026,6039 ****
        s.skind <- handle_choices (List.sort compare_choices sl) ;
        xform_switch_block b (fun () -> ref break_stmt) cont_dest i 
      end
    | Loop(b,l,_,_) -> 
            let i = get_switch_count () in 
            let break_stmt = mkStmt (Instr []) in
            break_stmt.labels <- 
! 						[Label((Printf.sprintf "while_%d_break" i),l,false)] ;
            let cont_stmt = mkStmt (Instr []) in
            cont_stmt.labels <- 
! 						[Label((Printf.sprintf "while_%d_continue" i),l,false)] ;
            b.bstmts <- cont_stmt :: b.bstmts ;
            let this_stmt = mkStmt 
              (Loop(b,l,Some(cont_stmt),Some(break_stmt))) in 
--- 6138,6152 ----
        s.skind <- handle_choices (List.sort compare_choices sl) ;
        xform_switch_block b (fun () -> ref break_stmt) cont_dest i 
      end
+ (*
    | Loop(b,l,_,_) -> 
            let i = get_switch_count () in 
            let break_stmt = mkStmt (Instr []) in
            break_stmt.labels <- 
! 	    [Label((Printf.sprintf "while_%d_break" i),l,false)] ;
            let cont_stmt = mkStmt (Instr []) in
            cont_stmt.labels <- 
! 	    [Label((Printf.sprintf "while_%d_continue" i),l,false)] ;
            b.bstmts <- cont_stmt :: b.bstmts ;
            let this_stmt = mkStmt 
              (Loop(b,l,Some(cont_stmt),Some(break_stmt))) in 
***************
*** 6043,6048 ****
--- 6156,6217 ----
            break_stmt.succs <- s.succs ; 
            let new_block = mkBlock [ this_stmt ; break_stmt ] in
            s.skind <- Block new_block
+ *)
+   | While (e, b, l) ->
+           let i = get_switch_count () in 
+           let break_stmt = mkStmt (Instr []) in
+           break_stmt.labels <- 
+ 	    [Label((Printf.sprintf "while_%d_break" i),l,false)] ;
+           let cont_stmt = mkStmt (Instr []) in
+           cont_stmt.labels <- 
+ 	    [Label((Printf.sprintf "while_%d_continue" i),l,false)] ;
+           b.bstmts <- cont_stmt :: b.bstmts ;
+           let this_stmt = mkStmt 
+             (While(e,b,l)) in 
+           let break_dest () = ref break_stmt in
+           let cont_dest () = ref cont_stmt in 
+           xform_switch_block b break_dest cont_dest label_index ;
+           break_stmt.succs <- s.succs ; 
+           let new_block = mkBlock [ this_stmt ; break_stmt ] in
+           s.skind <- Block new_block
+ 
+   | DoWhile (e, b, l) ->
+           let i = get_switch_count () in 
+           let break_stmt = mkStmt (Instr []) in
+           break_stmt.labels <- 
+ 	    [Label((Printf.sprintf "while_%d_break" i),l,false)] ;
+           let cont_stmt = mkStmt (Instr []) in
+           cont_stmt.labels <- 
+ 	    [Label((Printf.sprintf "while_%d_continue" i),l,false)] ;
+           b.bstmts <- cont_stmt :: b.bstmts ;
+           let this_stmt = mkStmt 
+             (DoWhile(e,b,l)) in 
+           let break_dest () = ref break_stmt in
+           let cont_dest () = ref cont_stmt in 
+           xform_switch_block b break_dest cont_dest label_index ;
+           break_stmt.succs <- s.succs ; 
+           let new_block = mkBlock [ this_stmt ; break_stmt ] in
+           s.skind <- Block new_block
+ 
+   | For (bInit, e, bIter , b, l) ->
+           let i = get_switch_count () in 
+           let break_stmt = mkStmt (Instr []) in
+           break_stmt.labels <- 
+ 	    [Label((Printf.sprintf "while_%d_break" i),l,false)] ;
+           let cont_stmt = mkStmt (Instr []) in
+           cont_stmt.labels <- 
+ 	    [Label((Printf.sprintf "while_%d_continue" i),l,false)] ;
+           b.bstmts <- cont_stmt :: b.bstmts ;
+           let this_stmt = mkStmt 
+             (For(bInit,e,bIter,b,l)) in 
+           let break_dest () = ref break_stmt in
+           let cont_dest () = ref cont_stmt in 
+           xform_switch_block b break_dest cont_dest label_index ;
+           break_stmt.succs <- s.succs ; 
+           let new_block = mkBlock [ this_stmt ; break_stmt ] in
+           s.skind <- Block new_block
+ 
+ 
    | Block(b) -> xform_switch_block b break_dest cont_dest label_index
  
    | TryExcept _ | TryFinally _ ->