T
tm
On 7/7/2011 5:51 PM, Peter Duniho wrote:
...
Not necessarily. I assumed a couple of decades ago that array index
checking would be impossibly inefficient, but it seems to work fine in
Java.
And in other languages, like Pascal, Ada and Seed7, as well.
I suspect that having integer range types would be a major help.
When I'm working out whether an int can overflow, I often think in terms
of the ranges of inputs to calculations. A compiler would be able to
tell that adding a digit to a digit always fits in the range [0,18].
I think there are two things:
1. range checks (like value fits in [0,18]).
2. check if an 32-bit (or 8-bit, 16-bit, 64-bit, ...)
computation overflows.
In the 1. case a compiler could generate code that does
the computation and checks the range afterwards.
In the 2. case a computation could result in wrong data,
because the overflow was silently ignored. In this case
either some checks must be done before the computation or
the overfow condition is recognized during or after the
computation. In an ideal world the hardware would do this.
A CPU could (in theory) easily recognize the overflow
and generate an interrupt. This way normal computations
(without overflow) would have no extra cost. AFAIK
commonly used CPUs do not have this possibility. They
have some FLAG, which is set when an overflow occurred.
But there is no possibility to cause an interrupt, when
the overflow FLAG is set. So code, which checks for
overflow, must check this flag after every computation.
Needless to say: Normal computations (without overflow)
are slowed down by this checks.
Because of this slow down most compilers and virtual
machines (AFAIK inluding the JVM) have no overflow
checking.
In other words: A missing hardware feature:
Trigger interupt when overflow flag is set.
Causes compilers and JVMs to omit overflow checks.
Greetings Thomas Mertes
--
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.