"Paul Rubin" <no.e...@nospam.invalid> wrote in message
news:7xy5imk...@ruckus.brouhaha.com...
> "Rod Pemberton" <do_no...@notemailnotz.cnm> writes:
...
> > Except for 'restrict' keyword, a pointer may point to or within any C
> > object, i.e., aliased.
>
> I thought the compiler could assume that (e.g.) local variables were
> unaliased, unless the code actually took the address. E.g., if you say
>
> int x = 3;
> foo();
> print(x);
>
> I have the impression the compiler is allowed to constant-propagate x,
> giving
>
> foo();
> print(3);
>
...
> obviously this is not allowed if you do something with &x that might
> change the value of x.
int x = 3;
int *y;
printf("%d\n",x);
y=&x;
*y=5;
printf("%d\n",x);
This prints 3 and 5 here ... Of course, the contents of 'x' is being
fetched in order to display it, due to the string conversion. Yes?
Odd ... I don't know if C can actually print or display a value without
going through a string conversion. I don't believe that there is a way. At
the moment, I can't recall any method to do so, if there is ... I think
that the ability to directly display an integer would be required in order
to do a constant-propagation to a print-like statement that you've
demonstrated.
So, the question becomes how do you detect if a constant has been propagated
or is being fetched for display? Any internal reference to 'x' in order to
check it's value would be detected by the compiler and might change the code
accordingly. So, this would require something outside the C context to
modify the contents of the 'x' variable. Of course, that something needs to
"know" exactly where to store the value. That leads into the problem of how
do you do know the location without referencing 'x' in a way that the C
compiler or linker can detect the reference, in order to not affect the
outcome ...
> To prevent the propagation from happening (among other uses), the
> "volatile" keyword was introduced.
Well, I'm not familiar with that, or perhaps forgot it ... A few years
back, I familiarized myself with "restrict". I know there were some changes
made for implementing "restrict", but I don't recall what they were. Maybe,
it's related to those changes. I don't use "restrict", keeping mostly to
ANSI C.
AIUI, the "volatile" keyword is to indicate that a variable's value can be
modified by something outside the C context. This prevents optimizing away
the read or write of a variables' value from or to memory, e.g., by holding
the value in a register. E.g., it can be used to ensure reading/writing of
correct values from memory mapped I/O devices, e.g., screen, accessing
values passed from assembly routines, e.g., interrupts, etc.
Rod Pemberton