Live data from Hacker News

Write Your Own Virtual Machine

justinmeiners.github.io

41–44 of 44 posts

Re: Write Your Own Virtual Machine

#41

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…

> All you have to do is perform the most basic of optimizations

I mean, of course this is possible, and every compiler everywhere hopefully optimizes this, but at the same time you need to understand why one is faster than the other in order to understand why it makes sense to optimize it. In the end, you’ll acquire a more in-depth understanding of these semantics.

Regarding the inner workings of a CPU, I wasn’t claiming you will completely understand a CPU, my god I hope you don’t because it’s a crazy rabbit hole.

Again, I was thinking about the higher level, like touching the top of the iceberg and seeing what’s in there. Many a programmer don’t have any idea how function calls work, or how functions return values by using the stack, etc etc.

Bottom line, just because you don’t learn everything, doesn’t mean you’ll learn something.

Re: Write Your Own Virtual Machine

#42
VMs have so much utility, so +1 to the author on the nerd nugget for compartimentalizing the use as a game cartridge.

VMs and VEs (environments) have gotten so portable that IOT and 5G will have to compete with raw storage (TB's) for the best user experience in a post-filtered world.

Possibly OT (or use case), but I've demod LTSP [0] to run multiple environments on a single server (PXE menu) and clustered multiple LTSP servers (bouncing to other LTSP menus by manually adding a server list to boot menu(s)) and treated them as books with chapters (each LTSP being a book).

Customizing each chapter by OS needs and media/content/apps has so many uses (immersive news,learning,entertainment) and paves the way for offnet productivity/entertainment that currently doesn't exist in the market (ala Home Library/Encyclopedia Server).

LTSP was not as PnP as it needs to be for consumer use (and external PCIe form factors were pricey), but anyone writing a VM (or decorating the interior) is already riding the next wave of physical data subscriptions/refills that bypass metered networks (SDCs or SSDs in TB capacities with OS agnostic data) for 8k video, VR and compilation/mixedtape datasets.

Portable VMs are a trend waiting to happen, so anything you can learn about it (inside-out) is not a waste.

[0] https://ltsp.org

Re: Write Your Own Virtual Machine

#43
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...

From my experience, writing your own gives you a level of control and leverage not achievable through a third party VM that might be worth it once you get some practice. As long as you haven't tried, you have no idea about the trade-offs, goes for anything.

Same goes for not compiling all the way down to byte code (or even worse, C), makes some things easier to implement and allows more flexibility because of the lack of separation between compile time and run time.

Re: Write Your Own Virtual Machine

#44

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…

Do you happen to have the source for this anywhere?
Post reply on HN