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

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

[avr-gcc-list] RE __attribute__((naked)) isn't doing fine!


From: sebastian meuren
Subject: [avr-gcc-list] RE __attribute__((naked)) isn't doing fine!
Date: Thu, 1 Jul 2004 09:50:08 +0200

Hello!

it's a bit complicated because I tried to discuss this topic at avrfreaks and 
in the mailing list.... so here you can find the avrfreaks-forum: 
http://www.avrfreaks.net/phpBB2/viewtopic.php?p=103885#103885

but concerning __attribute__((naked)):
it's not that good: it doesn't generate any normal prologue like building a 
stack frame, etc. but it still may insert code between the function label and 
the first user code. I posted my code into the avrfreaks-forum which showed 
this:

############################################################################################################################################

volatile void myfunc(void* arg) __attribute__ ((naked)); 
volatile void myfunc(void* arg){ 
asm volatile(
 "pop r25" "\n\t"
 "pop r24" "\n\t"); 


char tmp = (*(char*)arg); 
volatile char var = 0; 
while(tmp--){ 
var++; 
} 
asm volatile("reti"); 
} 

results in:
@00000029: myfunc
---- C:\avr\c-projects\tst/main.c -------------------
18:       volatile void myfunc(void* arg) __attribute__ ((naked)); 
19:       volatile void myfunc(void* arg){ 
+00000029:   2FF9        MOV     R31,R25          Copy register
+0000002A:   2FE8        MOV     R30,R24          Copy register
20:       asm volatile(
+0000002B:   919F        POP     R25              Pop register from stack
+0000002C:   918F        POP     R24              Pop register from stack
25:       char tmp = (*(char*)arg); 
+0000002D:   81E0        LDD     R30,Z+0          Load indirect with 
displacement
26:       volatile char var = 0; 
+0000002E:   8219        STD     Y+1,R1           Store indirect with 
displacement
27:       while(tmp--){ 
...
############################################################################################################################################
that especialy importand, because I want to pop the register r24/r25 from the 
stack _before_ they are moved to a diffrent location!!!
My attempt to solve this: (mail to Eric, answering my post to this list about 
codeorder / how to force the compiler to put two functions imidiatly behind 
oneanother):


Hello!

the question was the result of a diffrent one. So let's return to the real
problem. I'm developing a mt enviroment and every task looks like that:
void task(void* arg);

but because it's the entry-function (like main), the compiler doesn't have
to save any registers or so. I haven't thought about the stack-frame yet, so
that has to be considered (a remark @ avrfreaks.net - forum). But thr real
problem is loading the arg-pointer to r24/r25 __befor__ any task-code gets
executed! So I tried to declare the function as naked (no compiler
prologue!) and wrote my onwn one like "pop r25, pop r24" because my
stack-entry-frame looks like that: register - return/entry-address - void
*arg-pointer. So after entering my main-function, it has to pop the
registers from the stack. But what happened? The compiler inserted two
mov-instructions (mov x,r25, mov x,r24) right before my pop-prologue. So I
tried to find out how to force the compiler to add __my__ prologue to the
function, but all I found out is that this option doesn't exist. So I had to
work around that by tricking. All I did was to write a macro that generated
two functions for me:

if you declare s.th. TASK(task){ .... }, the result of the macro TASK is:
void task(void) __attribute__ ((naked));
void task(void){
    pop    r25
    pop    r24
}
void task__(void* arg) __attribute__ ((naked));
void task__(void*arg){
    ...
}

Because of the function-order in my source (the original-named
"task"-function before my real "task__"-function), the compiler first
compiles "task" (resulting in only two pop-instruction, no mov, etc. because
it hasn't got an argument!) and direct after it inserts the code for
"task__", the real function.. So I managed to write an epilogue, that is
executed before _any_ of the original function's code.... But this trick
depends on the assumption that the compiler uses the same code-order in it's
output like in it's input.... and I don't know what happends if you manualy
force s.th. to be at a certain address, etc., etc.
so my first question: can I define my own function-epilogue or a piece of
code that is inserted between the function label and it's first
instruction - has been left unanswered so far (it seems this feature is
missing)

my second one (are the two mov-instructions a compiler-bug?) hasn't been
asked/answered as well

what resulted in the final question concerning the code/function-order the
compiler generates!

I hope that wasn't too much for you (-; thank you for your help and
patience, Sebastian...



reply via email to

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