Sign in to follow this  
Nicholas Kong

How to build a virtual machine

Recommended Posts

Nicholas Kong    1535

I want to build a virtual machine because I want to really LEARN deeply how a virtual machine works underneath the hood. I have experiencing programming games in Java for a year and a half and love building software. I think my problem solving skills and curiosity can be transferred into tackling some complexity in building a VM. 

 

I read that I might need to communicate with the CPU and registers and RAM in some shape or form to create some features of a virtual machine. Does a library of Java have something that can necessarily communicate with hardware components of the system unit?

 

When I say build a virtual machine, I mean building the actual software using a library.

 

I need guidance. biggrin.png

Edited by warnexus

Share this post


Link to post
Share on other sites
Ravyne    14300

Depends entirely how you define 'VM' -- I assume what you're talking about is "something like the Java Virtual Machine" -- and even within the context of something like the JVM, there are several distinct approaches. The easiest approach would be some kind of high-level language or byte-code interpretter -- In that approach, the JVM is not unlike the BASIC programming languages of yore, or many of the scripting languages popular today. Byte-code interpreters are basically the same function, but are attractive because of increased performance of applications running on the VM, and code-density/code-hiding -- but now you also need to write some kind of compiler, in addition to your VM, both of which are big tasks.

A state-of-the-art VM today uses just-in-time (or JIT) compilation to get best performance. In this setup, there's typically a compiler that takes a high-level language and translates it into Intermediate Language (or IL), a lower-level representation (which often, but not always, looks similar to assembly language of popular CPUs); this compiler only concerns itself with high-level, algorithmic optimizations. Then, the JIT-employing VM performs a final phase of compilation in which the VM IL is compiled down to actual machine code that can be run on the host processor directly, but still within the sandbox defined by the VM platform.

 

Much of the job of writing a usable (professional) VM today is about security of the sandbox environment, things that enhance programmer productivity -- like garbage collection, and JIT performance. Within that sphere of influence, the part you probably think about as "the VM" is mostly about defining what the virtual hardware platform and IL look like -- things like whether the machine architecture is register-based/stack-based/direct-memory/something-else, to tedious and not-immediately-obvious concerns like what the threading model/shared-memory/message-passing/memory-coherence models are. That's for a 'production quality' kind of VM -- odds are, if you want to keep it simple, your VM will look more like a scripting language.

Share this post


Link to post
Share on other sites
Glass_Knife    8636

 http://amzn.com/1931841578

 

This is an older book, and has a terrible name.  It actually teaches how to build a scripting language compiler and virtual machine from the ground up.  It's written in C++, and not without some bugs in the code, but it is still a great book.  

 

I used this to make a scripting language, compiler, and virtual machine for my final project for my programming degree.  I was able to get math, variable, and recursive functions working, as well as embedding the language in C++ and adding C++ extensions to call C++ from the scripting language.

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

 http://amzn.com/1931841578

 

This is an older book, and has a terrible name.  It actually teaches how to build a scripting language compiler and virtual machine from the ground up.  It's written in C++, and not without some bugs in the code, but it is still a great book.  

 

I used this to make a scripting language, compiler, and virtual machine for my final project for my programming degree.  I was able to get math, variable, and recursive functions working, as well as embedding the language in C++ and adding C++ extensions to call C++ from the scripting language.

Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this