主要 C/C++ 生成的代码中的寄存器分配规则 编译器

发布于 2024-07-08 10:11:25 字数 298 浏览 6 评论 0原文

我记得以前(32 位 Intel 处理器之前)的一些规则,当时(至少对我来说)非常频繁地必须分析 C/C++ 编译器(在我的例子中,当时是 Borland/Turbo)生成的汇编输出查找性能瓶颈,并将汇编例程与 C/C++ 代码安全地混合。 比如使用 SI 寄存器作为 this 指针,AX 用于返回值,当汇编例程返回时应该保留哪些寄存器,等等。

现在我想知道是否有一些更流行的参考C/C++ 编译器(Visual C++、GCC、Intel...)和处理器(Intel、ARM...),如果没有,在哪里可以找到用于创建的部分。 有想法吗?

I remember some rules from a time ago (pre-32bit Intel processors), when was quite frequent (at least for me) having to analyze the assembly output generated by C/C++ compilers (in my case, Borland/Turbo at that time) to find performance bottlenecks, and to safely mix assembly routines with C/C++ code. Things like using the SI register for the this pointer, AX being used for return values, which registers should be preserved when an assembly routine returns, etc.

Now I was wondering if there's some reference for the more popular C/C++ compilers (Visual C++, GCC, Intel...) and processors (Intel, ARM, ...), and if not, where to find the pieces to create one. Ideas?

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

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

发布评论

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

评论(5

想念有你 2024-07-15 10:11:25

您正在询问“应用程序二进制接口”(ABI)和调用约定。 这些通常由操作系统和库设置,并由编译器和链接器强制执行。 Google 搜索“ABI”或“调用约定”。 来自 WikipediaARM 版 Debian

You are asking about "application binary interface" (ABI) and calling conventions. These are typically set by operating systems and libraries, and enforced by compilers and linkers. Google for "ABI" or "calling convention." Some starting points from Wikipedia and Debian for ARM.

顾挽 2024-07-15 10:11:25

Agner Fog 的“调用约定”文档总结了 Windows 和 Linux 64 和 32 位 ABI:http://www.agner.org/optimize/calling_conventions.pdf。 请参阅第 10 页的表 4,了解寄存器使用情况的摘要。

来自个人经验的一个警告:不要在内联汇编中嵌入有关 ABI 的假设。 如果您在内联汇编中编写一个函数,该函数假定在特定寄存器(例如 eax、rdi、rsi)中返回和/或参数传输,那么如果/当该函数被编译器内联时,它将中断。

Agner Fog's "Calling Conventions" document summarizes, amongst other things, the Windows and Linux 64 and 32-bit ABIs: http://www.agner.org/optimize/calling_conventions.pdf. See Table 4 on p.10 for a summary of register usage.

One warning from personal experience: don't embed assumptions about the ABI in inline assembly. If you write a function in inline assembly that assumes return and/or parameter transfer in particular registers (e.g. eax, rdi, rsi), it will break if/when the function is inlined by the compiler.

幻梦 2024-07-15 10:11:25

Open Watcom C/C++ 编译器支持两种调用约定:基于寄存器(默认)和基于堆栈(与其他编译器使用的非常接近)。 该编译器的用户指南对它们进行了描述,并且可以免费在线获取,以及编译器本身。 您可能会发现用户指南中的以下主题特别有用:

  • 10.4.1 使用基于寄存器的调用约定传递参数
  • 10.4.6 使用基于堆栈的调用约定
  • 10.5 基于 80x87 的应用程序的调用约定

Open Watcom C/C++ compiler supports two calling conventions, register-based (default) and stack-based (very close to what other compilers use). User's Guide for this compiler describes them both and is available for free online, together with the compiler itself. You may find these topics in the User's Guide especially helpful:

  • 10.4.1 Passing Arguments Using Register-Based Calling Conventions
  • 10.4.6 Using Stack-Based Calling Conventions
  • 10.5 Calling Conventions for 80x87-based Applications
天煞孤星 2024-07-15 10:11:25

好吧,今天如果打开优化,就没有了。 但是 GCC 允许您声明您的汇编指令应该使用特定的变量,无论它是否在寄存器中,甚至强制 GCC 将该变量放入您的指令可用的寄存器中。 您还可以声明您的内联汇编块为自己保留哪些寄存器(因此,如果需要,编译器应该围绕您的内联块生成适当的保存/恢复代码)

Well, today if optimisation is turned on, there arn't any. But GCC allows you to declare that your assembly instruction should use particular variable regardless if it's in register or not, or even to force GCC tu put that variable into a register usable with your instruction. You can also declare which registers your inline assembly block reserves for itself (so compiler should generate apropriate save/restore code around your inline piece, if needed)

能否归途做我良人 2024-07-15 10:11:25

I believe but am by no means sure that GCC uses the Itanium ABI for most of its function; the incompatibilites between it and the ABI it uses are documented.

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