Live data from Hacker News

Write Your Own Virtual Machine

justinmeiners.github.io

31–40 of 44 posts

Re: Write Your Own Virtual Machine

#31
post #27

Never wrote virtual machines, because I know why Sun (now Oracle) and Microsoft both spent a billion each to create theirs. You can write something that works over a weekend, but the performance won’t be good without JIT, generational GC, and many other extremely complicated optimizations. If you think you need to develop a VM, I recommend to reconsider, and think how you can reuse something that’s already there. For…

The point of writing your own vm isn’t to come up something that is on par with Sun’s or Microsoft’s vm, but rather have a hands on learning experience of the inner workings of a vm.

Real-life VMs don’t interpret, they JIT compile. The code in the article has nothing common with inner workings of real-life VMs.

Even VMs which do interpret don’t do the way written in the article, take a look: https://github.com/python/cpython/blob/v3.9.0rc1/Python/ceva...

What exactly it is you’re learning then?

Re: Write Your Own Virtual Machine

#32

I have written a virtual CPU from very simple building blocks and highly recommend doing it. I started with class Wire (which just wraps a bool) and class Transistor (which accepts two input Wire& and has an output Wire). It has an Update() function which sets the output state. From those I built up gates, then flipflops, registers, mux/demuxes, counters, an ALU, and memory banks. Then I wrote a machine language, con…

I did something similar, but with a LogicGate and a Wire class. I would recommend Morris Mano for those who would like to try.

It looks like those authors have a handful of computer engineering books. Is there one particular title you would recommend?

Re: Write Your Own Virtual Machine

#33

Earlier quoted context omitted.

I did something similar, but with a LogicGate and a Wire class. I would recommend Morris Mano for those who would like to try.

It looks like those authors have a handful of computer engineering books. Is there one particular title you would recommend?

sure, I think what I've used translates to the title "digital design"

Re: Write Your Own Virtual Machine

#34

I have written a virtual CPU from very simple building blocks and highly recommend doing it. I started with class Wire (which just wraps a bool) and class Transistor (which accepts two input Wire& and has an output Wire). It has an Update() function which sets the output state. From those I built up gates, then flipflops, registers, mux/demuxes, counters, an ALU, and memory banks. Then I wrote a machine language, con…

Cool. If you want to do that the easy way, use an electronics circuit simulator (https://en.wikipedia.org/wiki/List_of_free_electronics_circu...)

If you want a further challenge, write compiler optimization passes that recognize bit arrays a being bytes, figure out what parts are buses, binary adders, etc. and see how fast you can get this to run.

It probably would be fairer to do this on somebody else’s version of this, but I guess doing it on code you know already is hard enough, if you want to get compiler passes that likely would work on other simulated hardware, too.

Re: Write Your Own Virtual Machine

#35
Virtual machines are definitely fun, and can be useful things to know about if you ever design/implement a scripting language or an emulator.

I wrote a toy system a few years ago, a simple interpreter (C) along with a compiler/decompiler (Perl) to match it. Unfortunately my system didn't have a terribly well-designed instruction set. If I were to start over I'd probably implement 8086 instruction-set, or similar.

That said even a toy system is fun, the biggest issue with writing your own instruction-set is that you have to write the actual programs too. Which is often less fun! I rewrote my interpreter in golang recently, keeping the same instruction-set but adding a better trap-system. Of course re-implementing it meant that I still have the problem of no real programs being written for the machine!

https://github.com/skx/go.vm/

Re: Write Your Own Virtual Machine

#36
post #26

Earlier quoted context omitted.

It's a learning experience, it doesn't have to be production quality. It will teach you skills that are applicable in other contexts and give a deeper understanding of what makes a VM tick. And sometimes, a tiny VM is just what you need.

I agree about learning experience. But when I need a tiny language, I usually implement them so they compile into something already implemented and supported. Not necessarily .NET bytecode, here’s an example where I compiled a tiny language into HLSL source for the Microsoft’s shader compiler: https://github.com/Const-me/vis_avs_dx/tree/master/avs_dx/Dx...

Yeah sometimes that works. I wanted to write a "compiler" for brainfuck recently. So my first step was converting a BF program to C, then using gcc to compile that.

I guess my actual compiler wasn't so different, just generated an assembly source-file then passed that through an assembler. But that kind of transpiling is pretty simple to get working, and if your destination language is fast enough, or optimized enough, then it'll work just fine.

https://github.com/skx/bfcc/

Re: Write Your Own Virtual Machine

#37
post #27

Earlier quoted context omitted.

The point of writing your own vm isn’t to come up something that is on par with Sun’s or Microsoft’s vm, but rather have a hands on learning experience of the inner workings of a vm.

Real-life VMs don’t interpret, they JIT compile. The code in the article has nothing common with inner workings of real-life VMs. Even VMs which do interpret don’t do the way written in the article, take a look: https://github.com/python/cpython/blob/v3.9.0rc1/Python/ceva... What exactly it is you’re learning then?

> Real-life VMs don’t interpret, they JIT compile.

Pretty much every real-life JavaScript VM is tiered, and has an interpreter, which gathers data about expected usage which the JIT will use to inform its optimizations when it goes to generate machine code.

Still, you'd be surprised about the performance you can get out of a basic interpreter. Games have used Lua for years. I've written and reverse engineered plenty of custom bytecode for various reasons in the games space. It's a useful tool to have, and there are a lot of situations where performance either isn't the goal, or the large amount of tricks used by JITting VMs isn't helpful.

The dispatch loop you point to is just about using a C extension (computed gotos) to gain a few extra performance points. You can learn about it in about 30 minutes after knowing what a VM is.

Re: Write Your Own Virtual Machine

#38

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…

I thought that i++ ++i difference is related to the inner data structures of C++ iterators and that when using the former you need to keep 2 states compared to just one for that fraction of execution.

thanks for the downvotes, I'm out of here for good!

Re: Write Your Own Virtual Machine

#39

Earlier quoted context omitted.

I thought that i++ ++i difference is related to the inner data structures of C++ iterators and that when using the former you need to keep 2 states compared to just one for that fraction of execution.

thanks for the downvotes, I'm out of here for good!

What does ++ have to do with C++ iterators?

  int i = 0;
  i++;
How could that possibly involve an iterator? Moreover, C doesn't even have iterators.

Re: Write Your Own Virtual Machine

#40
post #39

Earlier quoted context omitted.

thanks for the downvotes, I'm out of here for good!

What does ++ have to do with C++ iterators? int i = 0; i++; How could that possibly involve an iterator? Moreover, C doesn't even have iterators.

You can use ++ on an iterator. Indeed you often do, e.g. in loops. ++iterator is preferable, and for consistency/so you don't have to think about its often recommended in C++ environments to just generally use pre-increment.

(Copying an iterator might be a lot more expensive than just copying an int, and might not be effectively optimized out by the compiler - e.g. if someone builds an iterator that's referenced-counted to an owning object or something)

Post reply on HN