home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / compiler / 1474 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.5 KB  |  75 lines

  1. Path: sparky!uunet!ogicse!das-news.harvard.edu!spdcc!iecc!compilers-sender
  2. From: eggert@twinsun.com (Paul Eggert)
  3. Newsgroups: comp.compilers
  4. Subject: Re: constant folding vs exceptions
  5. Keywords: parse, optimize
  6. Message-ID: <92-08-174@comp.compilers>
  7. Date: 28 Aug 92 20:59:16 GMT
  8. Article-I.D.: comp.92-08-174
  9. References: <92-08-114@comp.compilers> <92-08-163@comp.compilers>
  10. Sender: compilers-sender@iecc.cambridge.ma.us
  11. Reply-To: eggert@twinsun.com (Paul Eggert)
  12. Organization: Twin Sun, Inc
  13. Lines: 59
  14. Approved: compilers@iecc.cambridge.ma.us
  15.  
  16. henry@zoo.toronto.edu (Henry Spencer) writes:
  17. > In fact, ANSI C handed down a much stricter line on this:....
  18. > The only restriction is that if overflows are
  19. > visible, optimizations can't add or remove overflows.
  20.  
  21. Actually, in ANSI C, the behavior on overflow is undefined, so a
  22. conforming implementation optimization can remove overflows.
  23.  
  24. Spencer's right that the Ritchie compiler's treatment of integer overflow
  25. was broken, but unfortunately the C Standard lets a compiler behave in
  26. this way.  (C's not alone in this regard, of course; e.g. the Fortran
  27. standard has the same problem.)  That's too bad, since the problems that
  28. it leads to can be quite mysterious.  For example, in the following code:
  29.  
  30.     i = 0;
  31.     if (0 < j)
  32.         i = j;
  33.     assert (0 <= i);
  34.  
  35. integer overflow can cause the assertion to fail!
  36.  
  37. There's a trick to this, of course.  Here's a complete C program
  38. containing the above code.  Assume a 32-bit int.
  39.  
  40.     #include <assert.h>
  41.     int big = 2147483647;
  42.     main() {
  43.         int i, j;
  44.         j = big + 1;  /* This overflows.  */
  45.  
  46.         i = 0;
  47.         if (0 < j)
  48.             i = j;
  49.         assert (0 <= i);
  50.     }
  51.  
  52. Suppose the compiler optimizes `main's body into something like this:
  53.  
  54.         j = big + 1;  /* This overflows.  */
  55.  
  56.         i = 0;
  57.         if (0 <= big)
  58.             i = j;
  59.         assert (0 <= i);
  60.  
  61. The C Standard allows this optimization, because it works when `big + 1'
  62. does not overflow, and the code can do anything it pleases when `big + 1'
  63. _does_ overflow.  However, I suspect most programmers would say this
  64. optimization is incorrect, because it gives `i' a negative value when
  65. `big' is INT_MAX.
  66.  
  67. This is not a contrived example.  I derived the above program from a bug I
  68. encountered when building DEC SRC Modula-3 with GCC 2.2.2 -O on a Sparc.
  69. Happily, though, the GCC maintainers are programmers, not language
  70. lawyers; they've agreed that this behavior is a bug, and it'll be fixed in
  71. the next release.
  72. -- 
  73. Send compilers articles to compilers@iecc.cambridge.ma.us or
  74. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  75.