Forums

Wrapper function (C interface to asm routine on Blackfin)

Started by John McDermick July 15, 2011
On Fri, 15 Jul 2011 14:04:04 -0400, Roberto Waltman
<usenet@rwaltman.com> wrote:

>John McDermick wrote: >>... I wasn't able to find any information about how the C >>interface function arguments can be manually assigned to registers of >>the programmer's choice. >>The compiler is C/C++ Compiler for Blackfin (VisualDSP) > >In the general case, that is not possible. (I want to say never, but I >have not tried every C compiler under the Sun.) > >The compiler as a well defined calling mechanism, and the assembly >code needs to adapt to it, not the other way around. >I only used the SHARC family, not Blackfin, but took a peek at the >documentation installed with VisualDSP. > >Opening "blackfin.compiler.library.mn" and searching for "assembly" >lead me to the section titled "Calling Assembly Subroutines From C/C++ >Programs" > >They do not provide full details here, but they suggest, (and it is a >good suggestion,) to write a dummy C function with the same signature >of the assembly function you need. Then, inspect the generated >assembly code for the function to see how the arguments are accessed >*from the stack* Then you can copy them to registers if needed. > >Other relevant sections (sorry, no page numbers in that help file): > > "C/C++ Run-Time Model and Environment" > >and from there to: > > * Register usage conventions > > &#2013266067;Dedicated Registers&#2013266068; > > &#2013266067;Call-Preserved Registers&#2013266068; > > &#2013266067;Scratch Registers&#2013266068; > > &#2013266067;Stack Registers&#2013266068; > > * Program control conventions > > &#2013266067;Managing the Stack&#2013266068; > > &#2013266067;Transferring Function Arguments and Return Value&#2013266068; <=======
Just read your response to Rob Gaddi. I wrote my previous response thinking if new code. You may be able to write a *single* wrapper that just moves the parameters to where your old code expects then, calls the function, and moves the return value to where the Blackfin compiler expects it. Be careful with registers that must be preserved - That may also be different between the old and the new calling conventions. -- Roberto Waltman [ Please reply to the group. Return address is invalid ]
On 7/15/2011 10:43 AM, John McDermick wrote:
> >> >> I think the answer to that is pretty compiler specific; which are you >> using? Also, it might be easier to change your ASM routine to conform >> to the C calling convention for that platform than the other way around. >> > > If I have 100 asm routines (which at the time of their creation > adhered to > one calling convention) is moved to a system which has another calling > convention, it is probably easier just to change the interfaces > of those asm routines instead of changing 100 asm routines? Or am I > missing > something here?
Then it sounds like your wrapper may need to be assembly. Put the parameters you want into the registers they need to be in, branch to the sub, and then put the parameters back where C expects to find them. -- Rob Gaddi, Highland Technology Email address is currently out of order
On Jul 15, 2:34&#2013266080;pm, Rob Gaddi <rga...@technologyhighland.com> wrote:
> On 7/15/2011 10:43 AM, John McDermick wrote: > > > > >> I think the answer to that is pretty compiler specific; which are you > >> using? &#2013266080;Also, it might be easier to change your ASM routine to conform > >> to the C calling convention for that platform than the other way around. > > > If I have 100 asm routines (which at the time of their creation > > adhered to > > one calling convention) is moved to a system which has another calling > > convention, it is probably easier just to change the interfaces > > of those asm routines instead of changing 100 asm routines? Or am I > > missing > > something here? > > Then it sounds like your wrapper may need to be assembly. &#2013266080;Put the > parameters you want into the registers they need to be in, branch to the > sub, and then put the parameters back where C expects to find them.
That works fine provided none of the registers involved are sacred. It may also cost time. Jerry -- Engineering is the art of making what you want from things you can get.
Roberto Waltman wrote:

>Opening "blackfin.compiler.library.mn" and searchin for "assembly"
Sorry, that's the title once opened. The file name is "50_cc_bf.chm"
> how do I declare a wrapper function (or prototype) in C which loads the value I am calling with into register R2?
From the help file above: "Passing Arguments The details of argument passing are most easily understood in terms of a conceptual argument list. This is a list of words on the stack. Double arguments are placed starting on the next available word in the list, as are structures. Each argument appears in the argument list exactly as it would in storage, and each separate argument begins on a word boundary. The actual argument list is like the conceptual argument list except that the contents of the first three words are placed in registers R0, R1, and R2. Normally, this means that the first three arguments (if they are integers or pointers) are passed in registers R0 to R2 with any additional arguments being passed on the stack. " So, functions that match that signature (one parameter), this should do the trick: function(int dummy_param1, int dummy_param2, int actual_param) { ... } You still need to get the result into R0. -- Roberto Waltman [ Please reply to the group. Return address is invalid ]
John McDermick wrote:
> If I have some asm routine (lets call it _mysub) which ...just for the > sake of simplicity....adds 1 to what ever is in R2...how do I declare > a wrapper function (or prototype) in C which loads the value I am > calling with into register R2? > > I tried doing it like this: > > void wrapper(register r2) > { > _mysub; > }
If that's C, this declares a function which takes in 'int' parameter named 'r2' and does nothing :-> To do what you want to do, you can use asm. The syntax is 'asm("some asm instruction" : outputs : inputs)', so you can use "call _mysub;" for the asm instruction, and describe its inputs and outputs. If your calling conventions are so off-standard, you probably have different return registers, different caller-saved/caller-preserved registers, etc., so you'd have to describe those, too. Stefan