Forum > General

Statement as memory barrier for globals ("volatile")

(1/13) > >>

kupferstecher:
I found a discussion about why fpc doesn't need the volatile modifier for variables as it exists in C (Link below) and still is capable of doing optimisations.

My understanding is that global variables are loaded or stored for each statement they are used at least once (implicit "volatile" for globals). And on contrast local variables are free for any optimisations, also throughout several statements or may be even optimised away.

My question now is, which variables are 'global' and which are 'local' in the meaning of memory barriers?
A variable defined in the units interface section is global (of course).
A variable pointing to an absolute address is global (yes).
Is a public variable of a class or object 'global'?
Is a private variable of a class or object 'global'?
Is a class variable 'global'?
Is a variable defined in the implementation section 'global'?

So i want to know if there is anything to consider regarding optimisations when using interrupt handlers for example (target embedded).

Thanks!

I cite:

--- Quote ---In FPC, every statement is basically considered to be a compiler-level
memory barrier for accesses to global variables. So within a single
statement multiple accesses can be optimised, but that cannot happen not
across statements. It's not perfect since it inhibits some optimisation,
but on the flip side it's very simple and easy to reason about.
[...]
The reason the need for volatile on embedded systems is far less in FPC,
is the existence of the "absolute" keyword. In C, you have to declare
all of those memory mapped registers as volatile pointers, because
otherwise things will get optimised wrongly. In FPC, you can declare
them as variables that are "absolute" at some address, and the compiler
will never optimise their accesses. Only if you would take the address
of one of these variables and store it in a pointer, you would need
"volatile" in this context.
--- End quote ---
http://lists.freepascal.org/pipermail/fpc-pascal/2015-December/046055.html

Thaddy:
Volatile (C and derivatives) is the same as a Pascal writeable typed const ({$J+/-}

It indicates a type of var (typed const) that needs an allocation instead of being optimized away to a fixed - const - value.
It has a memory allocation on the heap instead of on the stack. Pascal had that *before* C and derivatives.

Example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program volatility;{$mode objfpc} function ShowVolatile:integer;{$push}{$J+}const t:integer = 0; // this equals a C type volatile....{$pop}begin  Result := t;  inc(t); // almost always an atomic operation for the native register sizeend; var i:integer;begin  for i := 0 to 9 do writeln(ShowVolatile);end.
Which also demonstrates that your informant was wrong: Pascal sometimes needs a "volatile" and the language provides it....

Note that on most - but not all - platforms this is atomic for the native register size or less... (And NO C isn't any better..... worse)

kupferstecher:
Thats interesting. Why seems that nobody knows that?
But how about the global variables, don't they provide the same?

Thaddy:

--- Quote from: kupferstecher on November 15, 2017, 04:58:35 pm ---Thats interesting. Why seems that nobody knows that?
But how about the global variables, don't they provide the same?

--- End quote ---
Yes and no: global variables are global. Volatiles aren't. In the above example the volatile T is not accessible on a global level. Scoping.
C used to use simple globals but that is at the risk of unintentionally modifying it. Pascal also allows that, though.

And.... really old hands know.... 8-) All of'm...

Maybe wikipedia helps (again, pretty accurate):
https://en.wikipedia.org/wiki/Volatile_(computer_programming)

Even after the wiki it can be confusing: scoping tells it all: a volatile can be scoped, whereas a global can not.

kupferstecher:
Scoping is clear so far.

I did a little testing on target AVR:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while not IsSet do;if IsSet is a local variable, then this will be kept as a register without address in the RAM, if its a field variable of an object, then it will be reloaded in each loop, even for the highest optimisation. For interrupt use this would normally be enough, but the question is if thats defined behaviour, or just a missing optimisation for that target.

Navigation

[0] Message Index

[#] Next page

Go to full version