Bug 25521

Summary: change semantics of const volatile variables
Product: gcc Reporter: Ulrich Drepper <drepper.fsp>
Component: middle-endAssignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: enhancement CC: etienne_lorrain, gcc-bugs
Priority: P3    
Version: 4.1.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2011-12-22 00:00:00

Description Ulrich Drepper 2005-12-21 18:57:51 UTC
In math code we often have to make sure the compiler does not fold operations at compile time.  In glibc we use variable declared as

  static const volatile double foo = 42.0;

The problem is that gcc moves such variables into .data.  But we could achieve that easily by leaving out the 'const'.  What is needed is a method to achieve volatile behavior while having the variable in .rodata (and .rodata.cst8 etc).

I therefore would like to ask for a change in the compiler which preserves the 'const' in the presence of 'volatile' and place the variable in read-only memory.
Comment 1 Gabriel Dos Reis 2005-12-21 19:17:51 UTC
Subject: Re:   New: change semantics of const volatile variables

"drepper at redhat dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| In math code we often have to make sure the compiler does not fold operations
| at compile time.  In glibc we use variable declared as
| 
|   static const volatile double foo = 42.0;
| 
| The problem is that gcc moves such variables into .data.  But we could achieve
| that easily by leaving out the 'const'.  What is needed is a method to achieve
| volatile behavior while having the variable in .rodata (and .rodata.cst8 etc).
| 
| I therefore would like to ask for a change in the compiler which preserves the
| 'const' in the presence of 'volatile' and place the variable in read-only
| memory.

that is tricky because of the general interpretation:

       [#10] EXAMPLE 1 An object declared

               extern const volatile int real_time_clock;

       may  be  modifiable  by hardware, but cannot be assigned to,
       incremented, or decremented.

If it may be modified by hardware or other external means, it can't go
into .rodata section.

-- Gaby
Comment 2 Ulrich Drepper 2005-12-21 19:38:55 UTC
Using gcc's section attributes won't fully work either.

Using __attribute((section(".rodata"))) is OK in the compiler, although the assembler (correctly) complaints.  But what is really needed is

__attribute((section(".rodata.cst8"))).  This will cause gcc to fail with an ICE.
Comment 3 Gabriel Dos Reis 2005-12-21 19:54:16 UTC
Subject: Re:  change semantics of const volatile variables

"drepper at redhat dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| __attribute((section(".rodata.cst8"))).  This will cause gcc to fail with an
| ICE.

That is compiler bug.  That is useful piece of data.  I believe the PR
should be retitled and reclassified based on that.

-- Gaby
Comment 4 Andrew Pinski 2011-12-22 01:15:48 UTC
*** Bug 51085 has been marked as a duplicate of this bug. ***
Comment 5 Andrew Pinski 2011-12-22 01:16:08 UTC
Confirmed.