bugAVR C Runtime Library - Bugs: bug #50987, Not all registers need to be...


You are not allowed to post comments on this tracker with your current authentication level.

bug #50987: Not all registers need to be declared volatile

Submitter:  Eric Tang <e_l_tang>
Submitted:  Tue 09 May 2017 08:20:25 PM UTC
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  None
Status:  None Assigned to:  None
Percent Complete:  0% Open/Closed:  Open
Release:  2.0.0 Fixed Release:  None

Sat 23 Dec 2017 10:33:22 AM UTC, comment #3: 

As Ambroz explained, removing volatile is only safe if there are no (side) effects. This case IMHO never happens (each register is there for a specific purpose/effect). The registers with the least hardware side effects probably are the GPIOn registers, which are effectively like global variables. When I use them, it's usually in ISRs, thus, they must be declared volatile.

Voting to close the bug as wontfix.

Tue 09 May 2017 08:51:41 PM UTC, comment #2: 

I think it is generally not a good idea to remove volatile from registers because writing/reading registers generally still has a side effect in addition to just changing the value.

For example, with volatile register accesses are never reordered (a different order may have different semantics).

Another example is that some infinite loops like while(1){REG=1;} would become undefined behavior (at least in C++) without REG being volatile. See: http://en.cppreference.com/w/cpp/language/memory_model (Progress guarantee).

In summary I think the only case where removing volatile from a register is where it has no specific hardware semantics at all - reading or writing has no side effect and the value itself does not influence anything.

Ambroz Bizjak <abizjak>
Tue 09 May 2017 08:33:26 PM UTC, comment #1: 

Would you be a /bit/ more specific?

Georg-Johann Lay <gjlayde>
Tue 09 May 2017 08:20:25 PM UTC, original submission:  

Many registers are no different from a global 8-bit variables in that they will always contain the values which were last written to them. The compiler will be able to better optimize code which accesses them if they are not declared volatile.

Eric Tang <e_l_tang>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by abizjak (Posted a comment)
  • -email is unavailable- added by gjlayde (Posted a comment)
  • -email is unavailable- added by e_l_tang (Submitted the item)

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13-3cd0.
    Corresponding source code