I do not see why overflow is any kind of impediment.
Whatever is in the register is what you get.
Physical machines that can trap on overflow do not
have to trap on overflow.
I'm familiar with the details of only a few platforms at that level,
none of which have that feature, so I cannot claim to know anything very
specific about what it would take to make a conforming implementation on
such a platform.
However, I believe that such platforms generally have to either be told
explicitly not to trap, or a handler must be set up for to intercept the
trap. If the platform has a good reason to make trapping the default, a
compiler would generally want to restore that default reasonably
quickly, so the code would have lots of "trap off", "trap on" command pairs.
In any event, that was not really the kind of thing I had in mind. What
I was thinking about was more subtle. Consider the following code, on an
implementation where INT_MAX == 32767, and INT_MIN == -32768:
int func(int a)
{
if(a < 300)
{
printf("a is small:%d\n", a);
}
return 200*a;
}
Because integer overflow has undefined behavior, and the value of 'a'
does not change anywhere between the if() statement and the return
statement, an implementation is permitted to build into it's translation
of this code the assumption that 200*a will NOT produce overflow. This
implies that -164 < a && a < 164. Therefore, the initial if() is
redundant, and can be optimized away. In other words, a fully conforming
implementation is permitted to optimize this code into the equivalent of:
int func(int a)
{
printf("a is small:%d\n", a);
return 200*a;
}
As a result, it's perfectly feasible for this code to be translated into
a program which prints out:
a is small:21000
This is not just a hypothetical possibility. Problems caused by real
defective code that triggered a similar fully conforming optimization by
a real and widely used compiler have been a topic for discussion in this
very forum within the past year or two. I think it involved
dereferencing a null pointer rather than integer overflow, but the basic
principle was the same. I can't remember enough of the context to
provide a proper citation - does anyone else recognize what I'm
referring to?