Nicola Scarabello
Nicola Scarabello

Reputation: 51

Does Arduino support tail call elimination?

I was wondering if the standard Arduino environment support tail call elimination... Does anyone know something about it?

Upvotes: 5

Views: 399

Answers (2)

Dmitry Grigoryev
Dmitry Grigoryev

Reputation: 3203

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.

Upvotes: 2

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.

Upvotes: 0

Related Questions