Register-based virtual machine?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
bakery2k
Posts: 2
Joined: Fri Dec 23, 2016 9:53 pm

Register-based virtual machine?

Post by bakery2k » Sun Dec 25, 2016 12:10 pm

I understand that MicroPython, like CPython, is built around a stack-based virtual machine and corresponding bytecode. Have there been any thoughts about the alternative: a register-based VM (as used by, for example, recent versions of Lua [1])?

In general, the performance of register-based machines is better than stack-based ones. For example, a register-based Java interpreter was found to require 25-35% less time to execute benchmarks than a stack-based interpreter [2]. The downsides of this register-based VM were increased bytecode size (by 25%) and increased complexity of the compiler and VM.

However, a recent paper [3] describes a register-based bytecode which is smaller than the equivalent stack-based code, and concludes:

"Because of the advantages in speed and its small bytecode size, a register-based virtual machine can be best applied on systems where both computational and storage facilities are limited (e.g. embedded systems)"

Would it be possible to add a register-based bytecode to MicroPython, either as a configuration option or a replacement for the current stack-based bytecode? Do you think this would produce a significant increase in performance?

If so, has anybody worked on implementing such a bytecode? Is this a feature which MicroPython might have in the future, or is the performance increase not worth the added complexity?

[1] https://www.lua.org/doc/jucs05.pdf
[2] https://www.usenix.org/legacy/events/ve ... -yunhe.pdf
[3] https://arxiv.org/pdf/1611.00467v1.pdf

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Register-based virtual machine?

Post by deshipu » Mon Dec 26, 2016 7:40 pm

On the other hand, stack-based bytecode is easier to generate, understand and debug. And the Python as a language has been created around a stack-based virtual machine precisely because of that. You could make a register-based machine, of course, but that wouldn't be Python anymore.

bakery2k
Posts: 2
Joined: Fri Dec 23, 2016 9:53 pm

Re: Register-based virtual machine?

Post by bakery2k » Tue Dec 27, 2016 1:02 pm

deshipu: "You could make a register-based machine, of course, but that wouldn't be Python anymore."

Are you saying that it would not be possible to use a register-based VM for Python (i.e. without changing the language)? I don't think that's true - for example, Dropbox's Pyston uses a register-based bytecode [1].

[1] http://blog.kevmod.com/2016/07/stack-vs ... or-python/

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Register-based virtual machine?

Post by deshipu » Thu Dec 29, 2016 9:54 am

Both stack-based and vm-based approaches have equal computational power (both are turing-complete), so of course you can implement in one of them whatever you can implement in the other -- in the worst case, you can emulate one in the other. So it's clearly technically possible to implement Python on a register-based machine. In fact, that's actually what happens, if you look not just at the Python VM, but at the combo of the Python VM and your computer's CPU.

What I meant with my comment was rather that the Python as a language is specified in terms of a stack-based VM -- the definition of the language makes assumptions. I believe that a language that would have the same syntax and semantics on the surface, but translated that into different mechanisms underneath would be different enough to warrant calling it a new language. But then again, I don't consider MicroPython to be a real implementation of Python either.

Post Reply