TI DSP:连接 C++和组装

发布于 2024-08-17 15:05:33 字数 736 浏览 6 评论 0原文

我发布了 这个问题访问 TI 的 28xx DSP 论坛,但尚未听到回复,我想这里可能有人知道。


我知道如何用汇编语言编写函数,以便它们可以用 C 语言调用;如果 C 调用名称为 foo(),则汇编函数名为 _foo()

如果我想使用 C++ 并优化汇编中的类方法怎么办?我该怎么做?我认为唯一的主要问题是:

  • 命名
  • 访问“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
};

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
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅渡 2024-08-24 15:05:33

使用平台上的标准调用约定,this 指针作为附加参数传递给函数。在我熟悉的所有平台上,它都作为第一个参数传递,但我没有进行大量 C++ 编码,因此我不确定标准是否保证这一点。您始终可以在您的平台上反汇编一些 C++ 代码来确认。

C++ 符号命名比 C 中的符号命名要痛苦得多,并且因编译器而异。我想您可以通过反汇编已编译的函数定义来找出要使用的正确符号名称,只需确保:该函数是正确类的成员,并且具有正确数量和类型的参数。

除非您确实需要在原位重现 C++ 函数,否则我可能只会创建一个标准 C 函数,并在其声明周围执行通常的 extern "C" { ... } 操作。

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.

無心 2024-08-24 15:05:33

您的编译器有内联汇编语法吗?如果有的话,它可能是最简单的选择,您可以让编译器处理函数命名和调用语法部分。

另外,Stephen 建议将 C++ 方法编写为“简单”C 函数调用的内联包装器,这也是一个不错的建议。 (您可能想让它只是一个普通函数,而不是您帖子中的静态成员函数,以获得一个简单的 C 接口。)

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.)

如歌彻婉言 2024-08-24 15:05:33

我会找到编译器相关标志并在 C++ 函数中编写程序集。通常有多种方法可以从汇编部分引用局部变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文