Live data from Hacker News

Write Your Own Virtual Machine

justinmeiners.github.io

1–10 of 44 posts

Re: Write Your Own Virtual Machine

#2
I once wrote my own virtual machine in college, complete with compiler and assembler, and I cannot recommend doing this enough. Especially the virtual machine part is not nearly as difficult as you would imagine, and to this day (15 years later) I still rely on the things i learned here.

The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a much better understanding of things like stacks, frame pointers and the overhead of calling a function. It will be completely obvious to you why “i++” is slower than “++i”.

Thanks for sharing this article.

Re: Write Your Own Virtual Machine

#3
I've just about finished writing a VM for the ZMachine (the VM that Zork was written for to make it portable). It's been tremendously enjoyable and I've learnt a lot.

I'm planning a series of how-to videos out of the project and will definitely be referencing this article to ensure my presentation of concepts is accurate. Thanks for sharing this.

Re: Write Your Own Virtual Machine

#4

I once wrote my own virtual machine in college, complete with compiler and assembler, and I cannot recommend doing this enough. Especially the virtual machine part is not nearly as difficult as you would imagine, and to this day (15 years later) I still rely on the things i learned here. The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a…

> It will be completely obvious to you why “i++” is slower than “++i”

...but it's not!

All you have to do is perform the most basic of optimizations: check, before generating code for an expression, if that expression is used. If not, then don't bother generating an intermediate for the result. Source: making a c compiler, just implemented this optimization today. (In an 'industrial-grade compiler', you probably want to elide this optimization, but do super complex control flow analysis on the resulting SSA to see that the intermediate is dead code. But for a toy compiler/vm, little tricks can save you a lot in codegen quality for little effort.)

> inner workings of a CPU

...if only. Lots of crazy stuff going on in a CPU that doesn't even start to come up in most VMs.

Re: Write Your Own Virtual Machine

#6

I once wrote my own virtual machine in college, complete with compiler and assembler, and I cannot recommend doing this enough. Especially the virtual machine part is not nearly as difficult as you would imagine, and to this day (15 years later) I still rely on the things i learned here. The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a…

[deleted]

Re: Write Your Own Virtual Machine

#7
I was tricked into writing a VM while doing Advent of Code last year and it really deepened my understanding of programming (e.g., on how to implement simple coroutines).

Advent of Code is supposed to be 25 daily programming puzzles leading up to christmas. I went in thinking they were going to be algo/datastructure tasks but half of them were actually about implementing and using a VM for a made up assembly language! The difficulty ramp was gradual enough that I did it with no background in compilers and it was lots of fun.

If you want to experience it, in last year's version https://adventofcode.com/2019 you write the VM in Days 2, 5, 7, 9 then apply it to solving problems in Days 11, 13, 15, 17, 19, 21, 23, 25.

I ended up loving the VM puzzles and ended up doing the AoC author's other challenges which have a similar theme: https://challenge.synacor.com/

Re: Write Your Own Virtual Machine

#8

I once wrote my own virtual machine in college, complete with compiler and assembler, and I cannot recommend doing this enough. Especially the virtual machine part is not nearly as difficult as you would imagine, and to this day (15 years later) I still rely on the things i learned here. The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a…

Why x++ was slower than ++x in the original C compiler maybe (and occasionally still when used inside another expression, but quite rarely these days on modern architectures and optimizing compilers).

Regardless, I much prefer to write ++x Because (a) I pronounce it “increase x” and I haven’t found a concise way to say “x’s current value but it is increased afterwards”. And (b) all other unary operators are prefix - easier to reason uniformly about things like order of evaluation.

In some contexts postfix is simpler - e.g. a memcpy-like implementation

    while (n—-) *t++ = *s++;
However, they are not very common in my experience.

Re: Write Your Own Virtual Machine

#9
post #7

I was tricked into writing a VM while doing Advent of Code last year and it really deepened my understanding of programming (e.g., on how to implement simple coroutines). Advent of Code is supposed to be 25 daily programming puzzles leading up to christmas. I went in thinking they were going to be algo/datastructure tasks but half of them were actually about implementing and using a VM for a made up assembly language…

Pro tip: advent of code almost always ends up with you writing a VM of some sort.

Re: Write Your Own Virtual Machine

#10
post #7

I was tricked into writing a VM while doing Advent of Code last year and it really deepened my understanding of programming (e.g., on how to implement simple coroutines). Advent of Code is supposed to be 25 daily programming puzzles leading up to christmas. I went in thinking they were going to be algo/datastructure tasks but half of them were actually about implementing and using a VM for a made up assembly language…

Funny that VM related days are all prime numbers :).

EDIT: No they're not, I wasn't fully awake, sorry.

Post reply on HN