I'm implementing a stack-based VM and I've been trying to read up on literature that explains or outlines algorithms for dealing with stacks no no avail. Here's an example:
int i = 3
int j = 4
int k = 5
Lets assume i
, j
, and k
are local variables so they would be traditionally stored on the stack. The assembly/bytecode translation would look something like:
pushi 3
pushi 4
pushi 5
And the stack would be: 5 4 3
I have an integer stack and a string stack, hence the pushi
, however my question is without storing these on the heap (with some ptr*
or literal identifier), how would a compiler or interpreter know that if I want to do something like int x = i + j
after the definition, I have to pop two and three times, respectively, as well as do my best not to lose k
(save it in a register or something, then push it back)?
I hope my question made some sense and there's probably a much smarter approach :P Thank you for any insight!