Skip to content

Educational implementation of a subset of the JVM bytecode to illustrate imperative and object-oriented programming.

Notifications You must be signed in to change notification settings

se-tuebingen/mini-jvm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Didactic JVM Implementation

This project implements a simplified Java Virtual Machine (JVM) in Java. The custom JVM can execute a subset of Java bytecode instructions, supporting basic operations such as arithmetic, control flow, and method invocations, as well as handling arrays and objects.

Limitations

  • Only int and reference types are supported
  • No constructors, so objects currently can only be constructed and need to be initialized manually since
  • No garbage collection
  • Only integer arrays
  • No inheritance

Overview

The custom JVM represents all values as integers, using the following interpretation:

  • If the expected type is an integer, the integer value is the value itself.
  • If the expected type is a reference, the integer value is the heap address.

The VM consists of:

  • A frame representing the current execution context.
  • A call stack of frames for managing method invocations.
  • A heap for storing objects and arrays.
  • A class table mapping class IDs to class definitions.

Instruction Set

For a documentation of instructions you can inspect the actual JVM specification (Some older version here).

The currently supported instruction set includes:

Simple Stack Machine Instructions

  • IConst(int value): Push an int constant onto the operand stack.
  • IAdd(): Add two integers.
  • ISub(): Subtract two integers.
  • IMul(): Multiply two integers.
  • Pop(): Pop the top value from the operand stack.
  • Dup(): Duplicate the top value on the operand stack.
  • Swap(): Swap the two top elements on the operand stack.

Local Control Flow

Simple Register Machine

Static Methods

Arrays

Objects

Example

Consider the following Java code:

var sum = 0;
sum = sum + 5;
sum = sum + 3;

This code can be represented using the instruction set as:

Instruction[] instructions = {
  new IConst(0),
  new IStore(0),
  new ILoad(0),
  new IConst(5),
  new IAdd(),
  new IStore(0),
  new ILoad(0),
  new IConst(3),
  new IAdd(),
  new IStore(0),
  new Return()
};

Potential Extensions

Students can extend the custom JVM in various ways, including but not limited to:

  1. More Simple Instructions:

    • Implement more bytecode instructions such as if_icmpge.
  2. Garbage Collection (GC):

    • Implement a simple garbage collector, such as mark-and-sweep, which will require changes to the memory layout.
  3. Additional Types:

    • Support other types. Be aware that types like long and double will require two registers and two slots on the heap.
  4. Constructors:

    • Implement support for constructors to initialize objects.
  5. Inheritance:

    • Add support for inheritance, allowing class hierarchies and method overriding.
  6. Translation of Actual Bytecode:

    • Implement a translation layer to convert actual Java bytecode into the internal representation of the custom JVM (a challenging task).
  7. Exception Handling:

    • Add support for exception handling using instructions like try, catch, throw.

...

About

Educational implementation of a subset of the JVM bytecode to illustrate imperative and object-oriented programming.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages