User:Kmcguire/JavaClassLoadAndExecute: Difference between revisions

Content deleted Content added
Pancakes (talk | contribs)
m added C code example of beginning VM
m Bot: Replace deprecated source tag with syntaxhighlight
 
(One intermediate revision by one other user not shown)
Line 1:
This page is about reading and interpreting Java bytecode. The code examples below are old and are by no means complete. However, they may serve some purpose as guidance. I have been working on a VM and have come a long ways from the code below, but also my code has become more complex and for someone who is interested in writting a VM for their OS the code below may prove to be much more useful than my full project.
You wanted to base your operating system with Java execution, but you were never quite sure how to get started? Well, here is some code (better here than sitting on my hard-disk) that can help give you an idea on how to get started.
 
You can find the project here. It is still simple by my effort but it is much larger. I hope to finish it to a point where it can be useful as a base for usage in my own toy OS. Or, useful in yours. I have made effort so far as to not reply on but the bare minimal of the standard library. Also, one day I will change out my ''malloc'' usage for a more in house solution.
The code is by no means complete. But, you can use it as a reference to sort of guide you through some potentially confusing documentation. There is no type checking for method calls. I used a simple trick to determine how many arguments are being passed to the method from the stack. You just walk backwards until you find an object reference then you reverse your list.
 
https://code.google.com/p/rhino-java-virtual-machine/
Also garbage collection is a big part omitted below. So your going to need a way to track objects referenced by each other, deal with cyclic references where two objects reference each object but no references exist on any stack. Also, the Java language is not type safe so depending on if you want a Java program to be able to crash your virtual machine would lead you to have different design on how the stack works and such as checks to prevent one object from being treated as another.
 
But... ''have fun is important too'', So implement what you want to implement. It all depends on the target goals of your OS. For example an embedded OS might not care as much if a program can crash the virtual machine (security problem). In that case your OS may have no security concerns but you still want to implement most of the machine features in a portable language. So keep your mind open for idea and hopefully the code below can help guide you into getting starting somewhere on building your virtual machine.
 
The most interesting part may likely be the actual loading of the class file which you can find below the method I used.
 
It can execute some basic operations so far. There is no ''java.lang''! I like to use Python to model out the entire program which lets me handle some of the pitfalls and traps. It gives me a good picture of what I need to do in C/C++.
 
Here are some resources which will allow you to implement a full virtual machine:
Line 30 ⟶ 24:
''Below is an example of loading multiple class files and interpreting some basic instructions. Also demonstrated is maintaining type information on the stack and in local variables. No garbage collection is needed for the Python code. In the C example code the infrastructure for garbage collection is there, but no code written to actually perform the collection.
== C Example ==
<sourcesyntaxhighlight lang="c">
#include <stdio.h>
#include <malloc.h>
Line 799 ⟶ 793:
return 1;
}
</syntaxhighlight>
</source>
== Python Example ==
<sourcesyntaxhighlight lang="python">
#!/usr/bin/python3.1
import os
Line 1,484 ⟶ 1,478:
print('------ return -------')
pprint.pprint(ret)
</syntaxhighlight>
</source>