3 ms·
> The 5.0 VM is a register machine, which operates on a set of virtual registers that can store and act on the local variables of a function, in addition to the
by ufo 4y ago
> The 5.0 VM is a register machine, which operates on a set of virtual registers that can store and act on the local variables of a function, in addition to the traditional runtime stack.
This is a common source of confusion, because the name "register machine" makes people think about CPU registers. However, the registers in a register VM are merely slots in the traditional runtime stack. The difference between a stack and register machine has to do with how the bytecode instructions are encoded. In a stack machine, most instructions implicitly pop arguments from and push their results to the top of the stack. The instructions are byte-sized, encoding just the operation name. For example, to add 10+10
LOADK 10
DUP
ADD
Meanwhile, in a register machine the instructions can read and write to any slot in the stack. Instructions are larger, because in addition to the operation name they also encode the indexes of the input slots and the index of the output slot. But it's worth it because you need less instructions in total and do less stack shuffling.
LOADK 1 10
ADD 1 1 2
- ithkuil 4y agoThis. Some CPU architecture even blur the line between hardware registers and stack slots: register windows! Indeed what matters is whether each instruction addresses operands explicitly (registers) or implicitly (top of the stack)
- xenadu02 4y agoThe funny part is all modern CPUs use register renaming (with a 100s deep register file) so they're effectively SSA machines if you squint.
- astrange 4y agoHaving so many registers but only a few register names helps because you don’t have to save/restore them all in context switches.
- ithkuil 4y agoThe main advantage is that you can keep the size of each instruction small. The more "architectural" registers, the more bits you need to encode each operand in all instructions. The larger the code, the more time is spent fetching the code from RAM and more i-cache is required. So how does having a larger "microarchitectural" register file help? Regiater renaming is used to eliminate false data dependencies arising from the reuse of registers by successive instructions that do not have any real data dependencies between them. This allows independent instructions to be executed in parallel even if seems that you'd have to wait until a regiater is freed up.
- cjohnson318 4y agoWhat's an SSA machine? Is it this, from Wikipedia? https://en.wikipedia.org/wiki/Static_single-assignment_form https://en.wikipedia.org/wiki/Static_single-assignment_form