[comp.std.c] Are externs automatically volatile?

apratt@atari.UUCP (Allan Pratt) (06/12/90)

Are all externs volatile?  Forgive me if this is obvious from the ANSI
spec, somebody took mine.  I compiled this code and got an unexpected
result:

	int this_is_extern;
	int my_array[MY_ARRAY_SIZE];

	int silly(void)
	{
		int i;
		
		for (i=0; i<this_is_extern; i++) {
			if (my_array[i] == 0) return i;
		}
		return -1;
	}

I had hoped, especially with -fstrength-reduce, that this_is_extern
would be read into a register once, rather than having the  memory
reference at the bottom of every loop, but that's not what happened. If
this_is_extern were also volatile, I would understand, and if it is
somehow implicitly volatile, that's the explanation.

Here's the assembly output from GCC 1.37.1 on a VAX:

	#NO_APP
	gcc_compiled.:
	.text
		.align 1
	.globl _silly
	_silly:
		.word 0x0
		clrl r0
		cmpl r0,_this_is_extern
		jgeq L6
		movab _my_array,r1
	L5:
		tstl (r1)
		jneq L3
		ret
	L3:
		addl2 $4,r1
		incl r0
		cmpl r0,_this_is_extern
		jlss L5
	L6:
		mcoml $0,r0
		ret

	.comm _my_array,400
	.comm _this_is_extern,4

============================================
Opinions expressed above do not necessarily	-- Allan Pratt, Atari Corp.
reflect those of Atari Corp. or anyone else.	  ...ames!atari!apratt

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/12/90)

In article <2213@atari.UUCP> apratt@atari.UUCP (Allan Pratt) writes:
>Are all externs volatile?

No, of course not.

>I had hoped, especially with -fstrength-reduce, that this_is_extern
>would be read into a register once, rather than having the  memory
>reference at the bottom of every loop, but that's not what happened.

Most compilers do not perform such global optimization over externs;
since under many circumstances the optimization would be incorrect,
the compiler writer finds it simpler to not worry about it.