I was wondering if the standard Arduino environment support tail call elimination... Does anyone know something about it?
Asked
Active
Viewed 337 times
5
-
1That's like asking "does a PC support tail call elimination?" - You're asking about the wrong component. You should be asking about a specific compiler. – Damien_The_Unbeliever Nov 09 '12 at 18:54
-
5@Damien_The_Unbeliever: he is asking about the *standard Arduino environment* which probably implicitly refers to a precise compiler. – Basile Starynkevitch Nov 09 '12 at 19:12
2 Answers
2
Tail call elimination is indeed supported and enabled by default in 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.

Dmitry Grigoryev
- 3,156
- 1
- 25
- 53
0
Most C compilers don't support tail call elimination. (that notion is not in the C standard).
Some recent C compilers may support it (only when optimizing strongly), in very limited cases. In particular, GCC (a recent version like 4.6 or 4.7).
You could try a simple C function and compile it and look at the generated assembly.

Basile Starynkevitch
- 223,805
- 18
- 296
- 547