aboutsummaryrefslogtreecommitdiffstats
path: root/cil.patch/cabs2cil.ml.patch
blob: 12a81ca5e7a04593c6271e3ceb5393b6e8cefeaa (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
*** ../cil/src/frontc/cabs2cil.ml	2006-05-21 06:14:15.000000000 +0200
--- ../cil_patch/src/frontc/cabs2cil.ml	2006-07-25 10:45:51.136500945 +0200
***************
*** 1,3 ****
--- 1,7 ----
+ (* MODIF: Loop constructor replaced by 3 constructors: While, DoWhile, For. *)
+ (* MODIF: Return statement no longer added when the body of the function
+           falls-through. *)
+ 
  (*
   *
   * Copyright (c) 2001-2002, 
***************
*** 816,828 ****
            (fun s -> 
              if s.labels != [] then 
                raise (Failure "cannot duplicate: has labels");
              (match s.skind with 
!               If _ | Switch _ | Loop _ | Block _ -> 
                  raise (Failure "cannot duplicate: complex stmt")
              | Instr il -> 
                  pCount := !pCount + List.length il
              | _ -> incr pCount);
              if !pCount > 5 then raise (Failure ("cannot duplicate: too many instr"));
              (* We can just copy it because there is nothing to share here. 
               * Except maybe for the ref cell in Goto but it is Ok to share 
               * that, I think *)
--- 820,835 ----
            (fun s -> 
              if s.labels != [] then 
                raise (Failure "cannot duplicate: has labels");
+ (*
              (match s.skind with 
!               If _ | Switch _ | (*Loop _*)
! 	       While _ | DoWhile _ | For _ | Block _ -> 
                  raise (Failure "cannot duplicate: complex stmt")
              | Instr il -> 
                  pCount := !pCount + List.length il
              | _ -> incr pCount);
              if !pCount > 5 then raise (Failure ("cannot duplicate: too many instr"));
+ *)
              (* We can just copy it because there is nothing to share here. 
               * Except maybe for the ref cell in Goto but it is Ok to share 
               * that, I think *)
***************
*** 838,843 ****
--- 845,851 ----
      let canDrop (c: chunk) =
        List.for_all canDropStatement c.stmts
  
+ (*
      let loopChunk (body: chunk) : chunk = 
        (* Make the statement *)
        let loop = mkStmt (Loop (c2block body, !currentLoc, None, None)) in
***************
*** 845,850 ****
--- 853,885 ----
          postins = [];
          cases = body.cases;
        } 
+ *)
+ 
+     let whileChunk (e: exp) (body: chunk) : chunk = 
+       let loop = mkStmt (While (e, c2block body, !currentLoc)) in
+ 	
+ 	{ stmts = [ loop ];
+           postins = [];
+           cases = body.cases;
+ 	} 
+ 
+     let doWhileChunk (e: exp) (body: chunk) : chunk = 
+       let loop = mkStmt (DoWhile (e, c2block body, !currentLoc)) in
+ 	
+ 	{ stmts = [ loop ];
+           postins = [];
+           cases = body.cases;
+ 	} 
+       
+     let forChunk (bInit: chunk) (e: exp) (bIter: chunk)
+                  (body: chunk) : chunk = 
+       let loop = mkStmt (For (c2block bInit, e, c2block bIter,
+ 			      c2block body, !currentLoc)) in
+ 	
+ 	{ stmts = [ loop ];
+           postins = [];
+           cases = body.cases;
+ 	} 
        
      let breakChunk (l: location) : chunk = 
        { stmts = [ mkStmt (Break l) ];
***************
*** 959,964 ****
--- 994,1000 ----
  
  
  (************ Labels ***********)
+ (*
  (* Since we turn dowhile and for loops into while we need to take care in 
   * processing the continue statement. For each loop that we enter we place a 
   * marker in a list saying what kinds of loop it is. When we see a continue 
***************
*** 971,980 ****
--- 1007,1035 ----
  
  let startLoop iswhile = 
    continues := (if iswhile then While else NotWhile (ref "")) :: !continues
+ *)
+ 
+ (* We need to take care while processing the continue statement...
+  * For each loop that we enter we place a marker in a list saying what
+  * chunk of code we must duplicate before each continue statement
+  * in order to preserve the semantics. *)
+ type loopMarker =
+     DuplicateBeforeContinue of chunk
+ 
+ let continues : loopMarker list ref = ref []
+   
+ let startLoop lstate =
+   continues := lstate :: !continues
+   
+ let continueDuplicateChunk (l: location) : chunk = 
+   match !continues with
+     | []                             -> E.s (error "continue not in a loop")
+     | DuplicateBeforeContinue c :: _ -> c @@ continueChunk l
  
  (* Sometimes we need to create new label names *)
  let newLabelName (base: string) = fst (newAlphaName false "label" base)
  
+ (*
  let continueOrLabelChunk (l: location) : chunk = 
    match !continues with
      [] -> E.s (error "continue not in a loop")
***************
*** 990,995 ****
--- 1045,1051 ----
      [] -> E.s (error "labContinue not in a loop")
    | While :: rest -> c
    | NotWhile lr :: rest -> if !lr = "" then c else consLabel !lr c !currentLoc false
+ *)
  
  let exitLoop () = 
    match !continues with
***************
*** 5465,5473 ****
--- 5521,5534 ----
                         * then the switch falls through. *)
                        blockFallsThrough b || blockCanBreak b
                     end
+ (*
                | Loop (b, _, _, _) -> 
                    (* A loop falls through if it can break. *)
                    blockCanBreak b
+ *)
+ 	      | While (_, b, _) -> blockCanBreak b
+ 	      | DoWhile (_, b, _) -> blockCanBreak b
+ 	      | For (_, _, _, b, _) -> blockCanBreak b
                | Block b -> blockFallsThrough b
                | TryFinally (b, h, _) -> blockFallsThrough h
                | TryExcept (b, _, h, _) -> true (* Conservative *)
***************
*** 5512,5518 ****
                | Break _ -> true
                | If (_, b1, b2, _) -> 
                    blockCanBreak b1 || blockCanBreak b2
!               | Switch _ | Loop _ -> 
                    (* switches and loops catch any breaks in their bodies *)
                    false
                | Block b -> blockCanBreak b
--- 5573,5579 ----
                | Break _ -> true
                | If (_, b1, b2, _) -> 
                    blockCanBreak b1 || blockCanBreak b2
!               | Switch _ | (*Loop _*) While _ | DoWhile _ | For _ -> 
                    (* switches and loops catch any breaks in their bodies *)
                    false
                | Block b -> blockCanBreak b
***************
*** 5522,5527 ****
--- 5583,5589 ----
                List.exists stmtCanBreak b.bstmts
              in
              if blockFallsThrough !currentFunctionFDEC.sbody then begin
+ (*
                let retval = 
                  match unrollType !currentReturnType with
                    TVoid _ -> None
***************
*** 5537,5542 ****
--- 5599,5605 ----
                  !currentFunctionFDEC.sbody.bstmts <- 
                    !currentFunctionFDEC.sbody.bstmts 
                    @ [mkStmt (Return(retval, endloc))]
+ *)
              end;
              
              (* ignore (E.log "The env after finishing the body of %s:\n%t\n"
***************
*** 5738,5743 ****
--- 5801,5807 ----
          doCondition false e st' sf'
  
      | A.WHILE(e,s,loc) ->
+ (*
          startLoop true;
          let s' = doStatement s in
          exitLoop ();
***************
*** 5746,5753 ****
--- 5810,5836 ----
          loopChunk ((doCondition false e skipChunk
                        (breakChunk loc'))
                     @@ s')
+ *)
+ 	(** We need to convert A.WHILE(e,s) where e may have side effects
+ 	     into Cil.While(e',s') where e' is side-effect free. *)
+ 	
+ 	(* Let e == (sCond , eCond) with sCond a sequence of statements
+ 	   and eCond a side-effect free expression. *)
+ 	let (sCond, eCond, _) = doExp false e (AExp None) in
+ 	  
+ 	  (* Then doStatement(A.WHILE((sCond , eCond), s))
+              = sCond ; Cil.While(eCond, (doStatement(s) ; sCond))
+ 	     where doStatement(A.CONTINUE) = (sCond ; Cil.Continue). *)
+ 	  
+           startLoop (DuplicateBeforeContinue sCond);
+           let s' = doStatement s in
+             exitLoop ();
+             let loc' = convLoc loc in
+               currentLoc := loc';
+               sCond @@ (whileChunk eCond (s' @@ sCond))
            
      | A.DOWHILE(e,s,loc) -> 
+ (*
          startLoop false;
          let s' = doStatement s in
          let loc' = convLoc loc in
***************
*** 5757,5764 ****
          in
          exitLoop ();
          loopChunk (s' @@ s'')
            
!     | A.FOR(fc1,e2,e3,s,loc) -> begin
          let loc' = convLoc loc in
          currentLoc := loc';
          enterScope (); (* Just in case we have a declaration *)
--- 5840,5866 ----
          in
          exitLoop ();
          loopChunk (s' @@ s'')
+ *)
+ 	(** We need to convert A.DOWHILE(e,s) where e may have side effects
+ 	     into Cil.DoWhile(e',s') where e' is side-effect free. *)
+ 	
+ 	(* Let e == (sCond , eCond) with sCond a sequence of statements
+ 	   and eCond a side-effect free expression. *)
+ 	let (sCond, eCond, _) = doExp false e (AExp None) in
+ 	  
+ 	  (* Then doStatement(A.DOWHILE((sCond , eCond), s))
+              = Cil.DoWhile(eCond, (doStatement(s) ; sCond))
+ 	     where doStatement(A.CONTINUE) = (sCond ; Cil.Continue). *)
+ 	  
+           startLoop (DuplicateBeforeContinue sCond);
+           let s' = doStatement s in
+             exitLoop ();
+             let loc' = convLoc loc in
+               currentLoc := loc';
+               doWhileChunk eCond (s' @@ sCond)
            
!     | A.FOR(fc1,e2,e3,s,loc) ->
! (*begin
          let loc' = convLoc loc in
          currentLoc := loc';
          enterScope (); (* Just in case we have a declaration *)
***************
*** 5784,5789 ****
--- 5886,5920 ----
          exitScope ();
          res
      end
+ *)
+ 	(** We need to convert A.FOR(e1,e2,e3,s) where e1, e2 and e3 may
+ 	     have side effects into Cil.For(bInit,e2',bIter,s') where e2'
+ 	     is side-effect free. **)
+ 	
+ 	(* Let e1 == bInit be a block of statements
+ 	   Let e2 == (bCond , eCond) with bCond a block of statements
+ 	   and eCond a side-effect free expression
+ 	   Let e3 == bIter be a sequence of statements. *)
+ 	let (bInit, _, _) = match fc1 with 
+           | FC_EXP e1 -> doExp false e1 ADrop 
+           | FC_DECL d1 -> (doDecl false d1, zero, voidType) in
+ 	let (bCond, eCond, _) = doExp false e2 (AExp None) in
+ 	let eCond' = match eCond with
+ 	  | Const(CStr "exp_nothing") -> Cil.one
+ 	  | _                         -> eCond in
+ 	let (bIter, _, _) = doExp false e3 ADrop in
+ 	  
+ 	(* Then doStatement(A.FOR(bInit, (bCond , eCond), bIter, s))
+            = Cil.For({bInit; bCond}, eCond', bIter, {doStatement(s); bCond})
+ 	   where doStatement(A.CONTINUE) = (bCond ; Cil.Continue). *)
+ 	  
+ 	  startLoop (DuplicateBeforeContinue bCond);
+           let s' = doStatement s in
+             exitLoop ();
+             let loc' = convLoc loc in
+               currentLoc := loc';
+               (forChunk (bInit @@ bCond) eCond' bIter (s' @@ bCond))
+ 
      | A.BREAK loc -> 
          let loc' = convLoc loc in
          currentLoc := loc';
***************
*** 5792,5798 ****
--- 5923,5932 ----
      | A.CONTINUE loc -> 
          let loc' = convLoc loc in
          currentLoc := loc';
+ (*
          continueOrLabelChunk loc'
+ *)
+ 	continueDuplicateChunk loc'
  
      | A.RETURN (A.NOTHING, loc) -> 
          let loc' = convLoc loc in