Re: compiler smarts: register variables and catching exceptions
andrew_nuss@yahoo.com wrote:
Hi,
I have a long running loop that uses 4 stacks whose pointers are
declared as register
variables and which catches a SpecialException. The question is
whether the
introduction of the catch block will destroy register optimizations,
and force the
compiler to ignore using registers to hold the stack pointers.
First of all, the 'register' keyword is in many cases obsolete, and
usually your compiler will decide without your help what live ranges of
the variables it allocates to registers.
Introduction of the catch block that uses the values of these
variables can prolong their live ranges, changing the compiler
behaviour. For a compiler, the
try
.......1
catch
......2
block is similar to
......1
if (unknown value)
.....2
and the reason is the possibility that an exception wouldn't occur. For
example, if you don't use the 'sp1' value in the try block but do use
it in the catch block, this value might be put somewhere if the catch
block exists, but it would be optimized away without the catch block.
---------------------------------------------------------------------------------------------------------------
do {
register int* sp1 = ...;
register int* sp2 = ...;
register int* sp3 = ...;
register int* sp4 = ...;
try {
do {
register int op = ...;
switch (op) {
// all of the cases do brief manipulations of the
various sp1,...,sp4
// some of the cases call functions which throw
SpecialException
Before any function call your compiler will either put a living (used
later) value of a variable to a register that keeps its value through
calls, or save it before a call and restore it sometimes after the
call. In the first case, the called function might save this value
elsewhere (usually on the stack) when it needs this register, and
restore it later, before return or raising an exception.
}
} while (true);
} catch (SpecialException& e) {
// code to cleanup the values currently "held" by the
sp1,...,sp4
// and then re-enter the outer loop
}
} while (true);
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]