TI DSP: interfacing C++ and assembly
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
};
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.
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.)
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.
精彩评论