I have a project in which I communicate with some devices using TWI interface. This project like many other of my projects is using a library for communication that was written long time ago and is reliable. Fun part is that in the middle of this project avr-gcc decided to optimize out every instruction that assings 0 to a variable. Let's look at his code:
_hd44780_data.expander_data[0] = 0x06;
_hd44780_data.expander_data[1] = 0x00;
_hd44780_data.expander_data[2] = 0x1F;
twi_write(HD44780_EXPANDER, _hd44780_data.expander_data,
3, &_hd44780_data.twi_status);
And gcc cuts out _hd44780_data.expander_data[1] = 0x00;
Code below, works fine.
_hd44780_data.expander_data[0] = 0x06;
_hd44780_data.expander_data[1] = 0x05;
_hd44780_data.expander_data[1] -= 0x05;
_hd44780_data.expander_data[2] = 0x1F;
twi_write(HD44780_EXPANDER, _hd44780_data.expander_data,
3, &_hd44780_data.twi_status);
I think that it has somethig to do with optimisation. I'm using avr-gcc (WinAVR 20100110) 4.3.3 These are my CFLAGS:
CFLAGS += -Os -mcall-prologues
CFLAGS += -ffreestanding
CFLAGS += -funit-at-a-time
CFLAGS += -finline-functions
CFLAGS += -fkeep-inline-functions
CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
CFLAGS += -fpack-struct
CFLAGS += -fshort-enums
CFLAGS += -Wall
CFLAGS += --std=gnu99
CFLAGS += -Wstrict-prototypes
CFLAGS += -mrelax
Another this is that this "optimisation" includes all instructions not only array assignments. What is mostly puzzling for me is that this behavior occurred in the middle of a project. Another thing is that this "optimisation" includes all instructions not only array assignments. What is mostly puzzling for me is that this behavior occurred in the middle of a project. I haven't changed any flags and I haven't updated toolchain.
But ancient Chinese adage says "With the two of you, the compiler is right.", so it would be much appreciated if anyone could point out where I might be going wrong.