Someone tell me why this wouldn't work...
Jul. 14th, 2009 05:43 pmVirtual machines work in one of two ways: either they use a stack and push/pop things on it, or they use registers.
Stacks are vastly easier to write because it more closely matches the way parsers work: you evaluate sub-expressions, push return values on to the stack, and so on. But you have to use more memory for the stack.
Registers are faster because they more closely match the way hardware works: a single register machine instruction encompasses two or four stack machine instructions, and you don't have to shuffle things around to get at things. But you have to deal with register allocation algorithms.
So, here's my idea: why not a VM with a variable number of registers? Each function would get run through some algorithm that figures out how many registers it needs to run, and then the VM just has that many registers for running that function. Function return values are always one register anyway (either the value or a reference to the value).
Stacks are vastly easier to write because it more closely matches the way parsers work: you evaluate sub-expressions, push return values on to the stack, and so on. But you have to use more memory for the stack.
Registers are faster because they more closely match the way hardware works: a single register machine instruction encompasses two or four stack machine instructions, and you don't have to shuffle things around to get at things. But you have to deal with register allocation algorithms.
So, here's my idea: why not a VM with a variable number of registers? Each function would get run through some algorithm that figures out how many registers it needs to run, and then the VM just has that many registers for running that function. Function return values are always one register anyway (either the value or a reference to the value).