compiler smarts: register variables and catching exceptions
22 Jun 2006 15:52:10 -0400

          From comp.compilers

Related articles
compiler smarts: register variables and catching exceptions (2006-06-22)
| List of all articles for this month |

Newsgroups: comp.compilers
Date: 22 Jun 2006 15:52:10 -0400
Organization: Compilers Central
Keywords: registers, optimize, question
Posted-Date: 22 Jun 2006 15:52:10 EDT


Assuming that either (1) my compiler supports the observance of the
register keyword for a pointer variable (buffer) or (2) my pointer
variables are so frequently used in the body of a function that my
compiler decides that it should use a consistent register for these
variables (as if it had obeyed the register hint), my question is what
happens with exceptions. For example, I want to catch a
SpecialException, and in the catch block, cleanup the data in the
pointer variables and resume looping, as follows:

void main ()
        // allocate stacks and create stack pointers
        // to hold int data and use registers
        // for the stack tops

        // give an explicit hint to use registers
        // but most likely the -fast compiler
        // mode would to the same thing

        register int* sp1 = ...;
        register int* sp2 = ...;
        register int* sp3 = ...;
        register int* sp4 = ...;

        do {

                try {
                        // tight inner loop
                        // which works thru opcodes
                        do {
                                int opcode = ...;
                                switch (opcode) {

                                        // the many cases of this switch
                                        // are lightweight and manipulate
                                        // the sp1...sp4 registers' data

                                        // some of the cases
                                        // can call functions, some
                                        // inlined, some not, which
                                        // throw SpecialException
                        } while (true);

                } catch (SpecialException& e) {

                        // handling to cleanup the data
                        // in the four stacks and resume
                        // processing back to the inner loop

                        for (int* p = base1; p != sp1; p++) {
                                int val = *p++;
                                ... manipulate val
                        ... manipulate sp1

                        for (int* p = base2; p != sp2; p++) {
                                int val = *p++;
                                ... manipulate val
                        ... manipulate sp2

        } while (true);

My concern is that the compiler, when it sees that the catch block
manipulates the stack pointer variables, will refuse to put them in
registers because of the difficulty in knowing how to restore
registers in preparing the catch block to have access to local
variables of scope. I.e. it will just restore the frame and decide
that sp1...sp4 must live in the frame since they are accessed in the
catch block. NOTE: the try block switch cases can throw a
SpecialException directly or indirectly thru inlined or non inlined

Is my concern valid? Will C++ compilers refuse to put sp1...sp4 into
registers? Is there a workaround?

[It may or may not be a problem, depending on how clever the compiler
is. These days, most compilers ignore register declarations because
they can figure out better than most programmers what variables would
benefit from being in registers. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.