Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created July 27, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/d8d8b1fd78679a07b47429f4cd6dda4d to your computer and use it in GitHub Desktop.
Save JoshCheek/d8d8b1fd78679a07b47429f4cd6dda4d to your computer and use it in GitHub Desktop.
Thinking about how tail call optimization might be implemented
#include <stdio.h>
int square(int n) {
return n * n;
}
int main(int argc, char const *argv[]) {
int a = 3;
int b = 2;
square(a);
square(b);
return 0;
}
/* REGISTERS
* stack pointer: 100
* instruction pointer: 001
* /* CODE: MAIN ...the "CODE" segment...
* 001 | push 3 <-- IP
* 002 | push 2
* 003 | push (SP-2)
* 004 | push 120
* 005 | pop
* 006 | push (sp-1)
* 007 | pop 120
* 008 | return 0
* /* CODE: DOUBLE2 ...
* 120 | push
* 121 | multiply
* 122 | return (SP-??)
* ...
* /* STACK:
* 201 | 000 // a <-- SP
* 202 | 000 // b
* 203 | 000
* 204 | 000
* 205 | 000
* 206 | 000
* 207 | 000
* ...
* ...and then we eventually find the heap...
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment