Created
July 27, 2017 15:13
-
-
Save JoshCheek/d8d8b1fd78679a07b47429f4cd6dda4d to your computer and use it in GitHub Desktop.
Thinking about how tail call optimization might be implemented
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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