[m-rev.] trivial diff: workaround cygwin C compiler bug

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Jan 29 14:12:49 AEDT 2003


On 29-Jan-2003, Mark Brown <dougl at cs.mu.OZ.AU> wrote:
> Wait, I think I see the problem.  We use the code addresses of those labels
> to look up the label layout info in a hash table, with the assumption that
> different labels will have different code addresses.  In this case,
> though, the code pointed to by the two labels happens to be identical,
> and the C compiler appears to recognise this and (quite rightly) merge the
> two pieces of code into one.

Hmm, interesting.

The C standard guarantees that "Two pointers compare equal if and only if
both are null pointers, both are pointers to the same object (...)
or function, [... or some cases involving arrays ...]".
This means that pointers to the same function don't compare equal,
even if the functions happen to do the same thing.

However, the GCC documentation of its address-of-labels extension
is silent on the issue of equality of label addresses.

Here's a small C program that demonstrates the issue.
Compiled with `-O0', it works fine, but when compiled with `-O2',
it gets an assertion failure.  (This is with gcc 3.1.)

	#include <assert.h>
	int i;
	void foo() {}
	int main() {
		void * labels[] = { &&label0, &&label1, &&label2, &&label3 };
		goto *labels[i];
		label0:
			assert(labels[2] != labels[3]);
		label1:
			return 0;
		label2:
			foo();
			goto label0;
		label3:
			foo();
			goto label0;

	}

Note that the assertion fails even if there is an `asm volatile("...");'
before each call to foo(), as is the case with the code that the Mercury
compiler generates in grade asm_fast.gc.  This optimization seems to
contradict the GCC documentation about "asm volatile" statements not
being "deleted, moved significantly, or combined".

But I recalling seeing on the GCC lists that a bug relating to incorrect
optimization of "asm volatile" was recently fixed.  So maybe this is
fixed in a more recent version of GCC.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list