Cause GCC to do address math at compile time

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If you do this:
Y_in_RAM = *&Y_in_ROM;
The compiler correctly interprets that dereferencing the reference to
Y_in_ROM is unnecessary, so it creates the machine code as if the
instruction were "Y_in_RAM = Y_in_ROM;".
Such as:
mov.w 0xf03e,w0
mov.w w0,0x093e

Although, in my case, Y_in_ROM is not a named variable.
The value I want is somewhere in a 1K swath of ROM that is copied out to an
external EEPROM chip on power up if a certain signature is not found in the
EEPROM chip.
This typically only happens once the first time the board is powered up at
the CM's factory.
Subsequently the user may alter some EEPROM values to customize operation of
the machine.
Such as changing the operation of the clock from a 12-hour day to a 24-hour
day.
The entire EEPROM contents are also mirrored into RAM so all reads of EEPROM
data may be read from RAM to avoid the I2C time delay that would be involved
if reads of EEPROM data had to always come from the external EEPROM chip.
All writes to EEPROM are handled by a function that immediately mirrors the
written data back into RAM so the RAM image is always a full and complete
copy of what is in EEPROM.
All variables in this EE_RAM area are named so they may be referenced by the
code.
The only reference to the EE_ROM data is the name EE_ROM_Base at the
beginning of the 1K of ROM at address 0xF000.
I also have a reference to the base of the EEPROM shadow RAM, named
EE_RAM_Base at address 0x900.
And of course I have a reference to the named variable Y_in_RAM at lets say
address 0x93E.

Now, if the user hits the button to return the machine back to the factory
fresh state, some but not all of the values in ROM must be copied back out
to the EEPROM chip.
So, what I need to do is copy lets say the clock selection data from ROM at
address 0xF03E to the variable Y_in_RAM.
So I try this:
Y-in_RAM = *(&EE_ROM_Base - &EE_RAM_Base + &Y-in_RAM);
But, instead of the compiler understanding that it knows all of the relevant
information at compile time which should essentially produce the same
machine code as above it creates machine code that includes doing all of the
math in registers to create a pointer and then sets Y-in_RAM to what is
pointed to, all at runtime.
So here is the question, is there a syntax that will clue the compiler to do
the pointer math at compile time to simply create machine language similar
to:
mov.w 0xf03e,w0
mov.w w0,0x093e

Can the compiler be induced to understand that the addresses &EE_ROM_Base
(0xF000), &EE_RAM_Base (0x900) and &Y_in_RAM (0x93E) are all constant so
that 0xF000 - 0x900 + 0x93E = the constant 0xF03E.
This should reduce to "Y_in_RAM = *(&0xF03E)";
Which should reduce to "Y_in_RAM = *&0xF03E";
Which should produce the machine code:
mov.w 0xf03e,w0
mov.w w0,0x093e
But for some reason, any syntax I try, always produces the machine code:
mov.w #0xf000,w0
mov.w #0x93e,w1
add.w w0,w1,w0
mov.w #0x900,w1
sub.w w0,w1,w0
mov.w [w0],w0
mov.w w0,0x093e
It is doing all of the pointer math at run time even though all values are
constant and known at compile time.


-- 
View this message in context: http://old.nabble.com/Cause-GCC-to-do-address-math-at-compile-time-tp33115582p33115582.html
Sent from the gcc - Help mailing list archive at Nabble.com.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux