[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

The VOLATILE Macro: X++ Suggestions & Improvements #1

This is the first in a series of X++ Suggestions & Improvements that
come out of meetings between MarkM and Chris.  They are a joint

We've repeatedly worried about the fact that virtually no compilers
implement the ANSI spec with respect to the interaction between local
variables and setjump.  The result is that any local variables that
are live in the block containing a SHIELD_UP (which internally must
expand to setjump) may not work correctly on longjmp should the
compiler have put them in a register.  The new suggestion is that we
have a macro VOLATILE in ccompatc.h (or perhaps xcompatx.hxx?) so


is taken to mean 

        Type varName = initExpr;

Except that in addition it must work around setjump (i.e., it must not
be in a register).  On an ANSI-conforming compiler, the expansion
above is sufficient.  On some platforms it may be:

        volatile Type varName = initExpr;

While on others it may be:

        Type varName;
        *&varName = initExpr;

This isn't strictly equivalent for class types--the first invokes the
Type::Type(Type&) constructor, while the second invokes Type::Type()
followed by Type::operator=(Type&).  The user of VOLATILE must be
aware of this.

On some platforms it may be:

        Type varName = initExpr;
        Type * ptr2_varName = &varName;

Since some compilers may conceivably optimize out ptr2_varName if it's
not used, and then proceed to put varName in a register, on these
platforms we might say:

        Type varName = initExpr;
        do_nothing (&varName);

Where "do_nothing" is a function in a separately compiled module which
the compiler can't tell actually does nothing (except by reading its
name ;->)