avr-gcc-list
[Top][All Lists]
Advanced

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

[avr-gcc-list] Optimization problems


From: Tvrtko A. Ursulin
Subject: [avr-gcc-list] Optimization problems
Date: Thu, 10 Oct 2002 10:16:34 +0200

Hello everyone!

This is my first posting to this list, and not only that, I have only recently 
started coding in avr-gcc.

I downloaded avr-gcc patch which applied cleanly to gcc 3.0.4.

I wrote a small kernel for a exercise. Mostly in C with small asm parts.
Everything works ok when compiled without -O, but with any -O tested (-Os, -O, 
-O2, -O9) it fails to store global variable back to RAM after manipulating 
it.

To clarify a bit. Below is the source code of two tasks that should run under 
my kernel. They don't do much, just run forever, and kernel preempts them, 
and gives control to other one.

They manipulate a global variable "gval" (at RAM 0x60), but with any 
optimization turned on, they never store that variable back in RAM. I suppose 
that is because the compiler sees it is a endless loop and thinks there is no 
point in doing that.

But I would really like to find out a way to solve this, because all my future 
tasks will be endless loops also, and code generated without optimizations is 
really suboptimal. As you can see in below asm listing... :(

Many thanks for any help!

--------------------------------------------------------------------
source.c
--------------------------------------------------------------------
void task1(void) __attribute__((naked));
void task2(void) __attribute__((naked));

uint8_t gval = 0x08;

void task1(void)
{
        outp(0xff,DDRB);
        for (;;)
        {               
                gval--;
                outp(~gval,PORTB);
        }
}

void task2(void)
{
        outp(0xff,DDRB);
        for (;;)
        {       
                gval++;
                outp(~gval,PORTB);
        }
}
--------------------------------------------------------------------
compiled with:
avr-gcc -g -mmcu=at90s8515 -Wall -Wstrict-prototypes 
--------------------------------------------------------------------
void task1(void)
{
  82:   cf 93           push    r28
  84:   df 93           push    r29
  86:   cd b7           in      r28, 0x3d       ; 61
  88:   de b7           in      r29, 0x3e       ; 62
        outp(0xff,DDRB);
  8a:   8f ef           ldi     r24, 0xFF       ; 255
  8c:   87 bb           out     0x17, r24       ; 23
  8e:   00 00           nop
        for (;;)
        {
                gval--;
  90:   80 91 60 00     lds     r24, 0x0060
  94:   81 50           subi    r24, 0x01       ; 1
  96:   80 93 60 00     sts     0x0060, r24
                outp(~gval,PORTB);
  9a:   80 91 60 00     lds     r24, 0x0060
  9e:   80 95           com     r24
  a0:   88 bb           out     0x18, r24       ; 24
  a2:   f6 cf           rjmp    .-20            ; 0x90
        }
}
  a4:   df 91           pop     r29
  a6:   cf 91           pop     r28
  a8:   08 95           ret

000000aa <task2>:

void task2(void)
{
  aa:   cf 93           push    r28
  ac:   df 93           push    r29
  ae:   cd b7           in      r28, 0x3d       ; 61
  b0:   de b7           in      r29, 0x3e       ; 62
        outp(0xff,DDRB);
  b2:   8f ef           ldi     r24, 0xFF       ; 255
  b4:   87 bb           out     0x17, r24       ; 23
  b6:   00 00           nop
        for (;;)
        {
                gval++;
  b8:   80 91 60 00     lds     r24, 0x0060
  bc:   8f 5f           subi    r24, 0xFF       ; 255
  be:   80 93 60 00     sts     0x0060, r24
                outp(~gval,PORTB);
  c2:   80 91 60 00     lds     r24, 0x0060
  c6:   80 95           com     r24
  c8:   88 bb           out     0x18, r24       ; 24
  ca:   f6 cf           rjmp    .-20            ; 0xb8
        }
}
  cc:   df 91           pop     r29
  ce:   cf 91           pop     r28
  d0:   08 95           ret
--------------------------------------------------------------------
compiled with:
avr-gcc -g -mmcu=at90s8515 -Wall -Wstrict-prototypes -Os
--------------------------------------------------------------------
00000082 <task1>:
        outp(0xff,DDRB);
  82:   8f ef           ldi     r24, 0xFF       ; 255
  84:   87 bb           out     0x17, r24       ; 23
        for (;;)
  86:   80 91 60 00     lds     r24, 0x0060
  8a:   80 95           com     r24
        {
                gval--;
  8c:   8f 5f           subi    r24, 0xFF       ; 255
                outp(~gval,PORTB);
  8e:   88 bb           out     0x18, r24       ; 24
  90:   fd cf           rjmp    .-6             ; 0x8c
  92:   08 95           ret

00000094 <task2>:
        outp(0xff,DDRB);
  94:   8f ef           ldi     r24, 0xFF       ; 255
  96:   87 bb           out     0x17, r24       ; 23
        for (;;)
  98:   80 91 60 00     lds     r24, 0x0060
  9c:   80 95           com     r24
        {
                gval++;
  9e:   81 50           subi    r24, 0x01       ; 1
                outp(~gval,PORTB);
  a0:   88 bb           out     0x18, r24       ; 24
  a2:   fd cf           rjmp    .-6             ; 0x9e
  a4:   08 95           ret
--------------------------------------------------------------------
compiled with:
avr-gcc -g -mmcu=at90s8515 -Wall -Wstrict-prototypes -Os
--------------------------------------------------------------------
00000082 <task1>:
        outp(0xff,DDRB);
  82:   8f ef           ldi     r24, 0xFF       ; 255
  84:   87 bb           out     0x17, r24       ; 23
        for (;;)
  86:   90 91 60 00     lds     r25, 0x0060
        {
                gval--;
  8a:   91 50           subi    r25, 0x01       ; 1
                outp(~gval,PORTB);
  8c:   89 2f           mov     r24, r25
  8e:   80 95           com     r24
  90:   88 bb           out     0x18, r24       ; 24
  92:   fb cf           rjmp    .-10            ; 0x8a
        }
}
  94:   08 95           ret

00000096 <task2>:
        outp(0xff,DDRB);
  96:   8f ef           ldi     r24, 0xFF       ; 255
  98:   87 bb           out     0x17, r24       ; 23
        for (;;)
  9a:   90 91 60 00     lds     r25, 0x0060
        {
                gval++;
  9e:   9f 5f           subi    r25, 0xFF       ; 255
                outp(~gval,PORTB);
  a0:   89 2f           mov     r24, r25
  a2:   80 95           com     r24
  a4:   88 bb           out     0x18, r24       ; 24
  a6:   fb cf           rjmp    .-10            ; 0x9e
        }
}
  a8:   08 95           ret


--------------------------------------------------------------------
compiled with:
avr-gcc -g -mmcu=at90s8515 -Wall -Wstrict-prototypes -O2
--------------------------------------------------------------------
        outp(0xff,DDRB);
  82:   8f ef           ldi     r24, 0xFF       ; 255
  84:   87 bb           out     0x17, r24       ; 23
        for (;;)
  86:   90 91 60 00     lds     r25, 0x0060
        {
                gval--;
  8a:   89 2f           mov     r24, r25
  8c:   81 50           subi    r24, 0x01       ; 1
                outp(~gval,PORTB);
  8e:   98 2f           mov     r25, r24
  90:   80 95           com     r24
  92:   88 bb           out     0x18, r24       ; 24
  94:   fa cf           rjmp    .-12            ; 0x8a
  96:   08 95           ret

00000098 <task2>:
        outp(0xff,DDRB);
  98:   8f ef           ldi     r24, 0xFF       ; 255
  9a:   87 bb           out     0x17, r24       ; 23
        for (;;)
  9c:   90 91 60 00     lds     r25, 0x0060
        {
                gval++;
  a0:   89 2f           mov     r24, r25
  a2:   8f 5f           subi    r24, 0xFF       ; 255
                outp(~gval,PORTB);
  a4:   98 2f           mov     r25, r24
  a6:   80 95           com     r24
  a8:   88 bb           out     0x18, r24       ; 24
  aa:   fa cf           rjmp    .-12            ; 0xa0
  ac:   08 95           ret


--------------------------------------------------------------------
compiled with:
avr-gcc -g -mmcu=at90s8515 -Wall -Wstrict-prototypes -O9
--------------------------------------------------------------------
00000082 <task1>:
        outp(0xff,DDRB);
  82:   2f ef           ldi     r18, 0xFF       ; 255
  84:   27 bb           out     0x17, r18       ; 23
        for (;;)
  86:   90 91 60 00     lds     r25, 0x0060
        {
                gval--;
  8a:   29 2f           mov     r18, r25
  8c:   21 50           subi    r18, 0x01       ; 1
                outp(~gval,PORTB);
  8e:   92 2f           mov     r25, r18
  90:   20 95           com     r18
  92:   28 bb           out     0x18, r18       ; 24
  94:   fa cf           rjmp    .-12            ; 0x8a
  96:   08 95           ret

00000098 <task2>:
        outp(0xff,DDRB);
  98:   2f ef           ldi     r18, 0xFF       ; 255
  9a:   27 bb           out     0x17, r18       ; 23
        for (;;)
  9c:   90 91 60 00     lds     r25, 0x0060
        {
                gval++;
  a0:   29 2f           mov     r18, r25
  a2:   2f 5f           subi    r18, 0xFF       ; 255
                outp(~gval,PORTB);
  a4:   92 2f           mov     r25, r18
  a6:   20 95           com     r18
  a8:   28 bb           out     0x18, r18       ; 24
  aa:   fa cf           rjmp    .-12            ; 0xa0
  ac:   08 95           ret



avr-gcc-list at http://avr1.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]