Jakob Engblom <jakob.engblom@iar.se> wrote:>I am presently investigating implementating a C compiler for a "pure">32-bit architecture. A problem which I have found is that when you>store chars and shorts (8 and 16 bit values) in the 32-bit registers,>you will have to look out for overflows in certain situations.

Bear in mind that there is no such thing, in C, as doing char or short
arithmetic. Values of those types are promoted to int when used in
expressions, so on a machine with 32-bit int, all arithmetic is 32
bits wide at least. The resulting values are narrowed only when they
are stored in char or short variables.

If you are trying to locate char and short variables in registers,
there is no problem with saying "the variable is the bottom 8 bits of
the register", and storing random trash in the upper 24 bits, but you
must make sure that the random trash does not become visible in
calculations. If c1 and c2 are 8-bit signed char variables, each
holding the value 125, the value of c1+c2 is 250, not -6 plus an
overflow (remember, the value is 32 bits wide).

The issue of doing narrow calculations arises only as an optimization,
when higher bits of the results are not visible at all... and it is
required to produce the same effects as doing calculations of the
normal width. So c1 = c1+c2 could be done using 8-bit arithmetic, but
only if its overflow behavior is the same as that of promoting the
values to 32 bits, doing 32-bit arithmetic, and assigning the result.
--
| Henry Spencer
| henry@zoo.toronto.edu
--