简体   繁体   English

Arduino 是否支持尾调用消除?

[英]Does Arduino support tail call elimination?

我想知道标准的 Arduino 环境是否支持尾调用消除……有人知道吗?

Tail call elimination is indeed supported and enabled by default in Arduino IDE.默认情况下,Arduino IDE 确实支持并启用了尾调用消除。 This is quite standard for micro-controller world where debug aids like proper stack frames are sacrificed for memory efficiency.这对于微控制器世界来说是非常标准的,其中为了内存效率而牺牲了诸如适当的堆栈帧之类的调试辅助工具。

Here's a test:这是一个测试:

const int RAM_SIZE_IN_BYTES = 2048;

void f(int i) {
  Serial.println(i);
  if(i == 0) return;
  else f(i-1);
}

void setup() {
  Serial.begin(9600);
  f(RAM_SIZE_IN_BYTES);
}

void loop() {
}

This code prints numbers from 2048 to 0 to the console using a recursive function, which (without tail call optimization) requires more nested calls than available RAM bytes.此代码使用递归函数将 2048 到 0 的数字打印到控制台,该函数(没有尾调用优化)需要比可用 RAM 字节更多的嵌套调用。

Most C compilers don't support tail call elimination.大多数 C 编译器不支持尾调用消除。 (that notion is not in the C standard). (该概念不在 C 标准中)。

Some recent C compilers may support it (only when optimizing strongly), in very limited cases.在非常有限的情况下,一些最近的 C 编译器可能支持它(仅在强烈优化时)。 In particular, GCC (a recent version like 4.6 or 4.7).特别是 GCC(最近的版本,如 4.6 或 4.7)。

You could try a simple C function and compile it and look at the generated assembly.您可以尝试一个简单的 C 函数并编译它并查看生成的程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM