Porting help, possible bug in AVR port

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Porting help, possible bug in AVR port

Post by Giovanni »

No because if you call a function from your ISR the compiler would assume the registers to contain trash after the call and it would generate the required saves in the prologue.
The compiler cannot see through function calls unless those are inlined, port_switch() is never inlined by design, it is into another module to begin with.

Giovanni
swinchen
Posts: 18
Joined: Tue May 15, 2012 8:44 pm

Re: Porting help, possible bug in AVR port

Post by swinchen »

Thank you for your patience.

That makes sense. I need to really start thinking more about what the compiler expects in terms of call-saved and call-used registers. I keep thinking that I have to do it all, while in reality the compiler will do most of it.

Sam
swinchen
Posts: 18
Joined: Tue May 15, 2012 8:44 pm

Re: Porting help, possible bug in AVR port

Post by swinchen »

Phew, I got my port up and working. It isn't really much more than extending the program counter and adding a few registers to extctx, but still it is working.

I hate to ask this but I have to: The ISR calls a function (port_switch) so why do we need to manually save the call-used registers (r18-r27,r30,r31 on AVR)?

...if you call a function from your ISR the compiler would assume the registers to contain trash after the call and it would generate the required saves in the prologue.


From that line of reasoning it seems as though the compiler should save the call-used registers in the ISR prologue that it generates?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Porting help, possible bug in AVR port

Post by Giovanni »

Good question :)

We don't save those registers, the macro...

Code: Select all

#define PORT_IRQ_PROLOGUE() {                                               \
  asm ("" : : : "r18", "r19", "r20", "r21", "r22", "r23", "r24",            \
                "r25", "r26", "r27", "r30", "r31");                         \
}


...is probably redundant. Note that it does not save registers, it just makes sure that the compiler saves them even in case a function is not called from the ISR (and this is never the case because port_switch() is always called).

Giovanni
swinchen
Posts: 18
Joined: Tue May 15, 2012 8:44 pm

Re: Porting help, possible bug in AVR port

Post by swinchen »

Muahaha! Thank goodness, I finally understand it. :)

Just to verify I commented out the "touching" of the registers and recompiled. The compiler still added those registers to the prologue. Thank you so much for walking me through how this all works. I have a much better grasp on it now.

Sam
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Porting help, possible bug in AVR port

Post by Giovanni »

You are welcome, ChibiOS is tricky at that level in order to achieve a good performance.

Giovanni
Post Reply