How to devise instruction set of a stack based machine?

393 Views Asked by At

Stack based virtual machines like CLR and JVM has different set of instructions. Is there any theory behind devising the instruction set while creating a virtual machine? e.g. there are JVM instruction sets to load constants from 0-5 onto the stack

iconst_0
iconst_1
iconst_2
iconst_3
iconst_4
iconst_5

whereas in CLR there are instruction set to load number from 0 to 8 onto the stack as follows

ldc.i4.0
ldc.i4.1
ldc.i4.2
ldc.i4.3
ldc.i4.4
ldc.i4.5
ldc.i4.6
ldc.i4.7
ldc.i4.8

why there is no ldc.i4.9 and if ldc.i4 <int32 (num)> is there why we need the above opcodes? And there are others like these.

I am eager to know what is the reason behind this difference between opcodes of different VMs? Is there any specific theory to devise these opcodes or it is totally driven by the characteristics of the VM itself or depends on the high-level language constructs?

1

There are 1 best solutions below

0
On

The typical answer is that because they are "bytecodes", there are only 256 of them, so you don't want to use up the whole range. You need to leave room for new bytecodes that were not considered at the time of initial design.

Thus what's typically done is a statistical analysis of what common patterns are and attempt to shorten them. (ie: instead of always having to use generic ldc, have iconst0, etc... as shorter / denser versions).

I would then guess that when designed, the CLR and JVM folks saw only a need for very small numbers.