On Sun, Aug 25, 2013 at 01:26:08AM -0700, Jonathan Kotker wrote: > Apologies for reviving an old thread, but I'm revisiting this issue, trying > to see if I can patch a way around it, without having to constant-fold > floating point numbers. > > Looking at the suggested line (line 2656 of src/cil.ml), it does not look > like that particular code should be throwing the error that I'm seeing, > because it checks if the type of the result is either an int or a float > (using 'integralType' on line 2650). Indeed, the error that I am seeing is > the following: > > C:/../testGlobalInit-gt.c:5: Bug: newTempVar called outside a function > C:/../testGlobalInit-gt.c:5: Error: global initializer > > Are there any suggestions on how to get this to work? Ideally, I guess I'm > looking for a way for expressions involving floating-point values to just > 'go through' without any changes and constant foldings, but it looks like > that's already happening.
The problem is that the ternary operator :? is not considered an expression in CIL (unless you use --useLogicalOperators, in which case it is represented by Question). So when CIL encounters this operator, it translates it into a conditional: {x = a ? b : c} becomes {if(a) x = b else x = c}. This translation does not work for global variables, because the C syntax does not allow you to use an if statement outside of a function. CIL tries as hard as it can to mitigate this issue by performing constant folding, in order to eliminate :? when it is safe to do so. When constant-folding fails, it falls back on the if trick, which requires (in general) a temporary variable, which cannot be allocated outside of a function, hence the error that you get. I don't see any way to solve this issue but using Question; it has been introduced precisely for those cases. Now, if you are reluctant to enable --useLogicalOperators as a whole, you can enable Question selectively with this trivial patch: diff --git a/src/frontc/cabs2cil.ml b/src/frontc/cabs2cil.ml index 7d7f3d9..d7fd2d9 100644 --- a/src/frontc/cabs2cil.ml +++ b/src/frontc/cabs2cil.ml @@ -4470,7 +4470,7 @@ and doExp (asconst: bool) (* This expression is used as a constant *) | Some e2' -> finishExp (se1 @@ se2) (snd (castTo t2 tresult e2')) tresult end - | CEExp (se1, e1') when !useLogicalOperators && isEmpty se2 && isEmpty se3 -> + | CEExp (se1, e1') when isEmpty se2 && isEmpty se3 -> let e2' = match e2'o with None -> (* use e1' *) snd (castTo t2 tresult e1') One could argue that Question should use regardless of --useLogicalOperators when constant folding fails. If you anyone feels this is a reasonable tweak to introduce in the next release, I'm willing to consider it (but it will of course change the fact that Question will not be optionnal anymore). Best regards, -- Gabriel ------------------------------------------------------------------------------ Introducing Performance Central, a new site from SourceForge and AppDynamics. Performance Central is your source for news, insights, analysis and resources for efficient Application Performance Management. Visit us today! http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk _______________________________________________ CIL-users mailing list CIL-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/cil-users