
A Virtual Machine - matt_d
http://www.craftinginterpreters.com/a-virtual-machine.html
======
morazow
If anyone is interested on making compilers / programming languages, there is
another recent initiative from Per Vognsen, Bitwise:
[https://github.com/pervognsen/bitwise](https://github.com/pervognsen/bitwise).

I occasionally watch streams so far it was very helpful to observe how he
makes design decisions, thought process and implementations.

------
munificent
Author here, and uncomfortably excited to discuss this with any and all who
are interested.

~~~
joe_the_user
One quick question.

You talk about a VM allocating memory. In the process of constructing my own
simple-as-possible VM, I figured allocation would be done by the routines
involved, with the VM just providing CPU-like facilities for accessing raw
memory. What are the advantages of a VM allocating memory?

~~~
chubot
We just discussed one aspect of this here:
[https://www.reddit.com/r/programming/comments/8a9zhz/impleme...](https://www.reddit.com/r/programming/comments/8a9zhz/implementing_a_stackbased_vm_in_c_part_of/dwxg5jp/?context=3)

There is no real way for the VM to avoid dynamically allocating memory. At the
very least, you have to allocate call frames (even if you know their size
statically).

Maybe the only way around this is if you restrict arbitrary recursion in the
language.

EDIT: Hm from the reply to that I was somewhat wrong...

~~~
joe_the_user
Thanks for the answer.

"No real way" is relative, of course. I happen to have constraints pushing for
a VM as small as conceivably possible without code-blow-up, like single-
instruction VM but implementing most logic/arithmetic primitives.

But which parts are dicier than others is worth considering.

~~~
chubot
Yeah I was a little wrong, and I edited my comment. But I am still confused by
your question. There are lots of places where you might need to allocate
memory -- it depends on the semantics of your source language, and it depends
what you consider part of the VM proper, etc.

It sounds like you are describing something with more primitive types, like a
CPU emulator or WebAssembly, rather than a dynamic language VM, which is the
subject of this book.

~~~
joe_the_user
Sure, that explains it. The VM has to allocate memory if it's interpreting a
dynamic language. Sorry if the question was elementary.

~~~
chubot
OK no problem, it would be interesting to see a comparison between the two
styles of VM! I remember that there is at least one talk here which goes into
the difference:

[http://soft-dev.org/events/vmss16/](http://soft-dev.org/events/vmss16/)

I forget what the different names he used. He distinguished between runtimes
like the JVM and runtimes like Xen, which emulate an operating system too. A
CPU emulator seems closer to Xen than the JVM.

\-----

EDIT: The names were "system" and "process" VMs, mentioned here:

[https://news.ycombinator.com/item?id=16777564](https://news.ycombinator.com/item?id=16777564)

So I think you are talking about a "system VM", where this book is more about
"process" VMs. They are related, but distinct -- it even says so in that
book's title!

It's definitely true that in something like Xen you would want to avoid
allocating memory more than in something like a JavaScript VM. (And it would
be easier to avoid allocating, too.)

I don't like that terminology, but it's a useful distinction.

------
UncleEntity
> Alas, the fastest [dispatching] solutions require either non-standard
> extensions to C, or hand-written assembly code.

I assume gcc's label addresses[0].

I've been planning to try those out the next time I get around to playing with
minischeme to see just how much of a speed difference they make -- I'd imagine
quite a bit since it could just hop around from opcode to opcode instead of
having to go through the loop (which I believe is outside the dispatching
function).

It's funny, searched the google to see if clang supported this and the top
result (from stackoverflow) has the comment "This is terrible coding style.
Why don't you use a switch or a function table. If you really want such code,
you should goto 70ies/80ies BASIC."

[0][https://gcc.gnu.org/onlinedocs/gcc/Labels-as-
Values.html](https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html)

~~~
munificent
> I assume gcc's label addresses[0].

Yes, that's definitely the main one I had in mind.

> I've been planning to try those out the next time I get around to playing
> with minischeme to see just how much of a speed difference they make

I added support for computed gotos to my bytecode interpreter for Wren and, if
I recall, it gave about a 10% speed boost on my (mostly micro-) benchmarks.
YMMV.

[https://github.com/munificent/wren/blob/master/src/vm/wren_v...](https://github.com/munificent/wren/blob/master/src/vm/wren_vm.c#L815)

------
the_arun
What is the software this site is made of? Wordpress? GitHub pages? or Custom?
It looks so cool. I am interested to know the details

~~~
colonelxc
All source is here.
[https://github.com/munificent/craftinginterpreters](https://github.com/munificent/craftinginterpreters)

~~~
the_arun
Thanks!

------
kiddico
This whole book looks like a good read! I'll have to check it out later when I
have some time.

~~~
runevault
I'm up to the chapter in part 1 on Classes and it has been a great experience
for reminding me how compilers work. Personally I recommend intentionally
doing it in a language other than Java just to force the extra step of
transliterating in your head, at least for me it has made the learning process
much stronger.

~~~
munificent
Yeah, I totally agree with that suggestion. Even handing typing in the code
samples yourself instead of just reading them can help you get out of the
"skimming" mindset and into really internalizing each step.

If you're curious people have ported the code in the book to a lot of
languages. Some of them are here:

[https://github.com/munificent/craftinginterpreters/wiki/Lox-...](https://github.com/munificent/craftinginterpreters/wiki/Lox-
implementations)

Though, if you want to do this yourself, consulting those might be considered
cheating. :)

------
ryanmccullagh
Having been researching process VMs for a few years now, most tutorial miss a
crucial piece: the garbage collector.

