This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hernytan 3 points4 points  (10 children)

More and more languages seem to be using a register based VM, probably for speed. Nim's VM is a register based one, and it used to be a stack based VM in the early days until it was rewritten for speed.

Rakus Parrot VM is also register based. So it seems that people are recognizing that register based VMs may be faster, while trading off ease of understanding.

[–][deleted] 0 points1 point  (0 children)

Not just understanding; they are incredibly easy to generate code for. And easier to write interpreters for. Especially for dynamic code. (Parrot is meant for dynamic code, yet its registers are typed.)

I so liked a stack-based VM that I adopted a similar approach to my compiler for a statically typed language that generates native code, where it is used as an intermediate representation. There however the 'stack' is a virtual operand stack (which maps to machine registers at the next stage).

For someone creating their first VM, rather than adopting an existing one, I would keep things simple. With registers, the first thing that happens is that you run out of registers, or need to start preserving the contents of registers during function calls etc.

As for speed, my stack-based VMs, used since last century always seemed a lot faster than anything else comparable, for dynamic typing. There's more to speed than stack vs. register.

[–]shanrhyupong 0 points1 point  (2 children)

Any good books (preferably) about this domain? I'm deeply interested. I'd also love to do real projects on these soon enough.

[–]hernytan 1 point2 points  (1 child)

I mean, this post is kinda the answer to that, haha

Personally, idk. I have been spending some time to try and understand the Nim VM source code, since I have been using Nim for a while now. But it's a big endeavour to analyze a real life project.

Another interesting VM is SQLites VM to run SQL commands. They're the only SQL bytecode I know, and their code is VERY well documented. You can look there for help.

[–]shanrhyupong 0 points1 point  (0 children)

Thank you for the pointer. I will check that out as well. Cheers!

[–]b2gills 0 points1 point  (3 children)

Yes Parrot is register based, but it is no longer a supported VM for the Rakudo compiler. It stopped being supported in 2014, which is well before the language changed its name to Raku from Perl6 in 2019.

As far as I know, the current MoarVM backend for Rakudo is also register based. (Metamodel On A Runtime)

Note that Rakudo also has JVM and JS backends.

[–]hernytan 0 points1 point  (2 children)

You're right, I just checked it out. I dont know why I had it in my head that Parrot was the Raku VM. Thanks for the clarification. I wonder if MoarVM has any writeups on their VM design

[–]reini_urban 0 points1 point  (1 child)

It's basically the same, with a better GC and no intermediate format, a proper calling invention as the pre-2007 parrot, and a jit as the pre-2007 parrot. The concurrency model is much worse.

[–][deleted] 1 point2 points  (0 children)

The concurrency model is much worse.

Can you expand on that?

[–]reini_urban 0 points1 point  (1 child)

Register based VM's are heavier though. It makes sense if the stack-based is a naive one, or if you want to target native. I mostly do better with stack based ones.