Hi, In our project we are using function pointers. After compiling the project, we got "RECURSIVE CALL TO SEGMENT" warning. By selecting OVERLAY (sfname ! *)in BL51, that warnings was removed. If we add function name in OVERLAY, will it allocate diffrent memory for local variables and function argumants?. How it is diffrent from by difining same function as rentrant function ?. Thanks in advance..
Rajesh, I'm really no expert on the topic, but I'll give it a crack. When you specify a function as "reentrant," you're telling the compiler NOT to pass function arguments in registers. This means that the compiler will generate code to behave more like a PC in that it will push arguments onto the stack and the function will pop them off. This is MUCH slower and more resource intensive than the standard methodology used by C51. Manually modifying the OVERLAY entries to fix problems caused by, say, function pointers has to do (as you rightly guessed) with how the compiler handles local variables. Normally, local variables for functions would be overlayed in all cases where on function doesn't call another, etc. to conserve memory space. When you use pointers, the compiler cannot figure out if/when you have called that function. This directive tells it just to be safe and not overlay the local automatic variables for that function with ANYTHING at all.
Thanks Jay Daniel. So, Which are the functions are being called indirectly(through function pointers)are should be defined using OVERLAY directive. we need not to define it as reentarnt. Am I missed any thing, please comment on this... Thanks Rajesh
Rajesh, I believe the general idea is that you can do one of two things. The first is to specify for the compiler which modules it should just skip doing overlaying for altogether. The other method is to take a look at your code and use the OVERLAY directive to help the compiler finish the call-tree that it would be otherwise unable to generate. As for the specifics, you'll have to look in the manual. If I could suggest an alternative, however: Could you simply use another function as a "dispatch" function that takes a parameter. For instance, you're using function pointers so that you could do something like:
functable[i]()
void dispatcher(unsigned char i) { switch(i) { case 1: a(); break; case 2: b(); break; } }
So, Which are the functions are being called indirectly(through function pointers)are should be defined using OVERLAY directive. Those that you, by (most likely faulty) human deduction, can figure out can use overlaid local variables. The "(most likely faulty) human deduction" is probably the most compelling reason to avoid function pointers when coding for the '51. There are means, as Jay show abive, to create "function selection friendly to the '51 architcture" so why not use those. That some ingnramus has told you that "C is C" you can file under Urban legends. Erik
If you know which functions will actually be called, you can use the OVERLAY directive to inform the compiler of the actual call tree. In this case, the compiler will overlay locals and parameters for the indirect call exactly as it would for a normal function call. Maintaining the overlay tree manually may be difficult and error prone. You might prefer the run-time expense of declaring all routines to be called through a pointer reentrant In this case, locals and parameters will be allocated on a stack -- not necessarily the 8051 stack pointed to by SP, but a stack maintained by software. See the manual and STARTUP.A51. In certain limited circumstances, the compiler is able to deduce the correct call tree even in the presence of function pointers. There's an app note on the topic. Typically this will be a constant table of functions, say event handlers for a state machine, all in the same linker segment as the caller. The linker sees a reference from the table to all the functions, and from the caller to the table, so it can build a call tree without help from the programmer. Separate compilation, where the called function isn't known to another module, usually requires manual intervention with the OVERLAY directive (or using the run-time stack with reentrant, or a coding technique that creates a static call chain, such as the dispatch function mentioned earlier).
Hi, Thanks for ur suggestions... If we wnat to use OVERLAY directive, we have to select OVERLAY (sfname ! *)in BL51 or NOOVERLAY in Misc controls. Is there any other way to define in .C file itself like #pragma... Thanks in advance
Don't even think of using function pointers in C51 before you've thoroughly read & researched the issues! Application Note 129 http://www.keil.com/support/docs/210.htm and several more...