GCC optmizing out code

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm doing a state machine to receive commands from the UASRT and this one case (code below) isn't working. Inspecting the lss output, it seems that GCC is optimizing out the buffer[Counter] = uart_tmp (check the output list after the code).

If I make buffer a global variable, GCC generates correct code. It doesn't need to be global, as it is only used inside the State_Machine function.

Any clues as why it is behaving like this?

Btw, I'm using WinAVR20100110 (dowloaded after 20/01/2010) and a m168 @ 18.432MHz.

void State_Machine (void)
{
	uint8_t uart_tmp;
	char buffer[33];		// 32 bytes + \0

...

		case SM_VFD_STR:
			uart_tmp = uart_getc();

			if (uart_tmp != 0x0D)		// 0x0D = Enter
			{
				buffer[Counter] = uart_tmp;
				Counter++;

				if (Counter > 32)
				{
					CurState = SM_ERROR;
					TipoErro = VFD_ERROR_STR;
				}
			}
			else
			{
				buffer[Counter] = 0;

				uart_puts(buffer);

				vfd_setstring(buffer);
				CurState = SM_END;
			}
			break;

...
		case SM_VFD_STR:
			uart_tmp = uart_getc();
     8f2:	9e dc       	rcall	.-1732   	; 0x230 
     8f4:	90 91 5e 01 	lds	r25, 0x015E

			if (uart_tmp != 0x0D)		// 0x0D = Enter
     8f8:	8d 30       	cpi	r24, 0x0D	; 13
     8fa:	69 f0       	breq	.+26     	; 0x916 <__stack+0x417>
			{
				buffer[Counter] = uart_tmp;		// <-- it's not generating code!
				Counter++;
     8fc:	89 2f       	mov	r24, r25
     8fe:	8f 5f       	subi	r24, 0xFF	; 255
     900:	80 93 5e 01 	sts	0x015E, r24

				if (Counter > 32)
     904:	81 32       	cpi	r24, 0x21	; 33
     906:	20 f1       	brcs	.+72     	; 0x950 <__stack+0x451>
				{
					CurState = SM_ERROR;
     908:	80 e1       	ldi	r24, 0x10	; 16
     90a:	80 93 40 01 	sts	0x0140, r24
					TipoErro = VFD_ERROR_STR;
     90e:	89 e0       	ldi	r24, 0x09	; 9
     910:	80 93 5f 01 	sts	0x015F, r24
     914:	1d c0       	rjmp	.+58     	; 0x950 <__stack+0x451>
				}
			}

Felipe Maimon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fmaimon wrote:
I'm doing a state machine to receive commands from the UASRT and this one case (code below) isn't working. Inspecting the lss output, it seems that GCC is optimizing out the buffer[Counter] = uart_tmp (check the output list after the code).

If I make buffer a global variable, GCC generates correct code. It doesn't need to be global, as it is only used inside the State_Machine function.

To know for sure,
it would be helpful to see the rest of your code.
I'm guessing that it's a big switch.
In that case, the data stored is never used.
If you want to preserve it across function calls,
you probably want it static.

BTW I rarely use if(!=) else.
My preference is for single negatives.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you got it... It has be static. It wasn't a problem when the whole switch was inside main while(1) loop. Now it is in its own function, called by the endless loop, doing one char at each run.

I'll just leave it as a global variable.

Thank you skeeve.

Felipe Maimon