gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Weird GCC optimization bug? [solved]

#178116 - asoggytoaster - Mon Oct 28, 2013 2:56 am

I'm not sure how many people are still around to view this and provide input but I thought I'd post it anyway. Now, I know that this isn't the optimal way of doing this, but http://pastebin.com/YagHSBkM in that code snippet, I created a struct with bit-fields to access the various values of the display control register at 4000000h.

I don't have a problem with bit juggling,I have a macro display_set_mode() that will set the register value without using the bitfields, instead it relies on the .raw member of the struct. but, to the point..

If you look carefully at the assembly generated by the compiler, you'll notice that lines 64-65 of the paste are consistent with line 56. Then lines 66-69 are consistent with line 58. The compiler optimized line 57 out of existence, which in this instance is kinda bad because line 57 sets the display mode.

Now, when optimization is completely disabled, the compiler will generate code that functions as expected. But, when any optimization is turned on at all, line 57 disappears. Has anyone seen anything like this before?


Last edited by asoggytoaster on Mon Oct 28, 2013 5:05 am; edited 1 time in total

#178117 - Dwedit - Mon Oct 28, 2013 4:28 am

Did you forget to make it volatile? You need to declare the pointer as volatile if it's a memory-mapped register, otherwise GCC thinks it's regular memory and will skip unnecessary writes.

Or in other words, something like this:

#define display_control_register (*(volatile display_control_reg_t *)BASE_REG)
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#178118 - asoggytoaster - Mon Oct 28, 2013 4:47 am

Oops. Yes, that seems to be where I made the oversight. I'm working with vim and have quite a few files open - I did make the other register pointers volatile, I guess I just forgot to mark this one that way. Thanks for the help!