The Case for Virtual Register Machines
Created on 2023-10-02T23:42:32-05:00
Stack VMs such as Java's have "local variables" which are identical to registers anyway, despite posessing an instruction stack to compute on.
Spilling: moving data out of registers to memory when you have run out of registers and a more important one needs to be used
Stack registerization
Assign a register number to positions of the evaluation stack.
Walk through the byte stream and simulate running the opcodes. When an instruction would pop two numbers and push one, look at the register number assigned to the stack positions and replace it with the number. So an 'a b -> a' stack diagram becomes 'add 0 1 0'.
Stack silliness
Sometimes the Java machine says nonsense like "STORE 6, LOAD 6," which is intended to copy a value from the stack to local variable #6 while retaining it on the stack. You may need an optimization pass to go over and fix those.