Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Arduino support tail call elimination?

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

like image 492
Nicola Scarabello Avatar asked Nov 04 '22 11:11

Nicola Scarabello


1 Answers

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.

like image 125
Dmitry Grigoryev Avatar answered Nov 26 '22 22:11

Dmitry Grigoryev