views:

170

answers:

4

I posted this Q to TI's 28xx DSP forum but haven't heard a response and figured maybe someone here might know.


I know how to write functions in assembly so that they are C-callable; if the C-callable name is foo() then the assembly function is named _foo().

What if I want to use C++ and optimize a class method in assembly? How do I do that? I assume the only major issues are:

  • naming
  • accessing the "this" pointer
  • accessing class members by somehow knowing offsets

and if I don't want to worry about the last two, then perhaps I would write a static member function and do this:

class MyClass
{
  int x;
  static int _doSomething(int u); // implement this in assembly
public:
  inline void doSomething() { x = _doSomething(x); } 
  // lightweight C++ wrapper to handle the class member / "this" pointer stuff
};
+5  A: 

The this pointer gets passed as an additional argument to the function, using the standard calling convention on your platform. On all the platforms I'm familiar with it is passed as the first argument, but I don't do a lot of C++ coding, so I'm not sure if this is guaranteed by the standard. You can always disassemble some C++ code on your platform to confirm.

The C++ symbol naming is rather more painful than in C, and varies from compiler to compiler. I suppose you could figure out the right symbol name to use by disassembling a compiled function definition, just make sure that: the function is a member of the right class, and has the right number and type of arguments.

Unless you really need to reproduce a C++ function in situ, I would probably just make a standard C function and do the usual extern "C" { ... } around its declaration.

Stephen Canon
I agree, it sounds much simpler writing a standard C function that is used internally by the method of the class. However, one may have to accept an extra method call then, I don't know if that matters.
Skurmedel
@Skurmedel: Generally the C++ wrapper around the extern "C" function will simply be calling the C function with the additional `this` parameter made explicit. Have the C++ wrapper be "inline" and you'll likely avoid the extra method call.
Michael Burr
Good point, didn't think about inlining the C++ method.
Skurmedel
@Stephen: TI compiler guarantees certain calling conventions so for instance in the 28xx series the registers XAR4 and XAR5 are used for the 1st and 2nd pointer arguments. They don't talk explicitly about `this` but I have verified that it acts the same as if `this` is a hidden first argument.
Jason S
Accepted since the answer I got on the TI message board is similar.
Jason S
+1  A: 

Does your compiler have an inline assembly syntax? If you have that, it may be the easiest option, and you can let the compiler handle the function naming and call syntax pieces.

Alternately, Stephen's suggestion of writing the C++ method as an inlined wrapper around a "simple" C function call is a good one. (You probably want to make it just a plain function, not a static member function as in your post, to get a simple C interface to it.)

Brooks Moses
TI's DSP compiler does have inline assembly but the interfacing between C/C++ data structures and assembly is very poor. (vs. Microchip GCC which uses extended assembly syntax and automatically interfaces between C/C++ variables and symbolic quantities used in assembly.)
Jason S
A: 

I would find the compiler-dependent flag and write the assembly within the C++ function. Usually there are ways to reference local variables from within the assembly section.

Robert
not within TI's 28xx compiler. I've looked.
Jason S
That is a MAJOR FAIL for that compiler.
Robert
yeah, tell me about it. :(
Jason S
A: 

what happens is that I need to open a library that allows me to work on DSP (digital signal processing) and so far and could not find. if I can help with this is the thanks a lot

alex