compiling to python byte codes
"Martin v. Löwis"
martin at v.loewis.de
Fri Sep 3 09:42:58 CEST 2004
Maurice LING wrote:
>> If you're using registers, I guess you'd have to store the values in
>> variables, and load/store them through the stack whenever you do an
>> operation -- maybe with some optimization if you can keep the result
>> on the stack.
> I dont't quite get this right. Since x86/PPC uses register operations,
> why do virtual machines, like python's and java's, are designed as stack
> machines? Why not just stick to registers?
I really think you should study programming language implementations
for some time before approaching your problem.
For an interpreter, what the processor does is completely irrelevant
(not completely if you have a just-in-time compiler, as that needs
to generate machine code, but totally irrelevant if you have an
interpreter). Using a stack-based implementations allows to simplify
the opcodes - many opcodes don't need parameters, or atmost a single
parameter. This allows to survive with less than 256 opcodes, which
is the source for calling these opcodes "byte code". That, in turn,
allows for an implementation that uses an "interpreter loop", which
consists of a "giant switch".
In x86, a single instruction has between 1 and 20 bytes, and the
decoding process (finding out what the instruction does) is
very lengthy. For a microprocessor, this doesn't matter, since it
is done in hardware, and in parallel with executing other
instructions (pipelining). For an interpreter, the decoding process
must be superfast, and therefore supersimple.
More information about the Python-list