T
Tim Rentsch
I have a question about what ANSI C allows/requires in a particular
context related to 'volatile'. Consider the following:
volatile int x;
int
x_remainder_arg( int y ){
return x % y;
}
Suppose we had a machine that doesn't have a remainder
instruction and the compiler implements remainder using
the re-writing
a % b :=: a - a/b*b
Let's assume for the sake of discussion that the identity
is ok in the value sense - so if 'a' and 'b' are values,
the expression on the right hand side yields the correct
value. My question is how does 'x' being 'volatile' affect
things? For example, if 'x_remainder_arg' were compiled
as though it were written like this (and no optimization):
int
x_remainder_arg_1( int y ){
int t = x;
return t - t/y*y;
}
that seems alright. But what if 'x_remainder_arg' were
compiled as though it were written like this:
int
x_remainder_arg_2( int y ){
return x - x/y*y;
}
Note that 'x', which is 'volatile', is referenced only
once in the original function, but has two references
in the compiled code.
Now for my questions:
1) Does the ANSI C standard _permit_ an interpretation
like 'x_remainder_arg_2' where two references to 'x'
are made in the compiled code when only one reference
to 'x' is made in the source? If so, why?
2) Does the ANSI C standard _forbid_ an interpretation
like 'x_remainder_arg_2' where two references to 'x'
are made in the compiled code when only one reference
to 'x' is made in the source? If so, why?
Please note that I am not asking what the compiler
ought to do, or what the standard ought to permit
or to forbid. I'm asking only what the standard
does permit or does forbid.
If different versions of the ANSI standard say different
things on these questions I'd like to know that too.
Please identify which version is being referenced if
that's relevant.
thanks!
context related to 'volatile'. Consider the following:
volatile int x;
int
x_remainder_arg( int y ){
return x % y;
}
Suppose we had a machine that doesn't have a remainder
instruction and the compiler implements remainder using
the re-writing
a % b :=: a - a/b*b
Let's assume for the sake of discussion that the identity
is ok in the value sense - so if 'a' and 'b' are values,
the expression on the right hand side yields the correct
value. My question is how does 'x' being 'volatile' affect
things? For example, if 'x_remainder_arg' were compiled
as though it were written like this (and no optimization):
int
x_remainder_arg_1( int y ){
int t = x;
return t - t/y*y;
}
that seems alright. But what if 'x_remainder_arg' were
compiled as though it were written like this:
int
x_remainder_arg_2( int y ){
return x - x/y*y;
}
Note that 'x', which is 'volatile', is referenced only
once in the original function, but has two references
in the compiled code.
Now for my questions:
1) Does the ANSI C standard _permit_ an interpretation
like 'x_remainder_arg_2' where two references to 'x'
are made in the compiled code when only one reference
to 'x' is made in the source? If so, why?
2) Does the ANSI C standard _forbid_ an interpretation
like 'x_remainder_arg_2' where two references to 'x'
are made in the compiled code when only one reference
to 'x' is made in the source? If so, why?
Please note that I am not asking what the compiler
ought to do, or what the standard ought to permit
or to forbid. I'm asking only what the standard
does permit or does forbid.
If different versions of the ANSI standard say different
things on these questions I'd like to know that too.
Please identify which version is being referenced if
that's relevant.
thanks!