pytunium
A simulator for programs written in the assembler language of
pytunia, the JVM assembler of the
lecture Formal Languages and Their Applications (Chapter 12).
The window has three columns:
- Program: the assembler program. The next instruction is highlighted
in yellow, the instruction executed before in blue.
- Stack: the stack of the JVM. The cell that
SP points to (the top of
the stack) is orange, the cell that LV points to (the first argument of
the current method) is blue. Each frame is marked with the name of its
method.
- Working area: what the current instruction does. For
iadd, the two
operands move from the stack into the working area, the sum appears, and
the sum moves back onto the stack.
Below the columns, the output area shows what the program prints, and the
warnings and errors of the simulator.
Installation
pytunium is available on PyPI:
pip install pytunium
This installs pytunia, too: it checks the
syntax of the program before the simulation starts, so the error messages are
the same as when assembling.
If several versions of Python are installed, install pytunium for the Python
that you want to start it with:
python3 -m pip install pytunium # macOS and Linux
py -m pip install pytunium # Windows
To update pytunium to the newest version:
pip install --upgrade pytunium
pytunium needs Python 3.12 or newer with Tkinter. The installers from
python.org and Anaconda contain Tkinter; with Homebrew, install it with
brew install python-tk, on Debian/Ubuntu with sudo apt install python3-tk.
To check whether Tkinter works, run python3 -m tkinter: it opens a small
window.
The development version from GitHub, which may contain changes that are not
on PyPI yet:
pip install git+https://github.com/karlstroetmann/pytunium
Starting the simulator
pytunium Sum.jas # or: python3 -m pytunium Sum.jas
Without a file, the example Sum.jas from Chapter 12 is shown. The examples
Sum.jas, Hello.jas and Primes.jas (generated by the compiler of
Chapter 13) are in the folder pytunium/examples of the repository, and of the
installed package.
Options: --font-size N (default 14) and --speed N (1 = slow, 10 = no
animation).
Controls
| Button / key |
Effect |
| Play / Return |
runs the program, animated with the chosen speed |
| Step / Space, → |
executes one instruction |
| Step Over / ⇧Space, ⇧→ |
like Step, but a method call is executed completely (see below) |
| Step Out / ↑ |
executes the rest of the current method (see below) |
| Stop / Return, Esc |
stops Play |
| Reset / ⌘R (Ctrl+R) |
reloads the file from disk and starts again |
| Open… / ⌘O (Ctrl+O) |
loads another program |
| Speed |
1 (slow) … 9 (fast), 10 = no animation |
| click on a line number |
sets or removes a breakpoint (Play stops before it) |
| ⌘+ / ⌘− / ⌘0 |
larger / smaller / normal font (useful for a projector) |
| mouse wheel on the stack |
scrolls the stack |
Reset reads the file again, so you can edit the program in an editor and
press Reset to simulate the new version.
Step Over and Step Out are named after the buttons of the same names in
debuggers.
- Step Over: if the next instruction is
invokestatic, the whole call is
executed, including all the calls made inside it, and the simulation stops
at the instruction after the call. The call and the return are animated;
the body of the method runs without animation. For any other instruction,
Step Over does the same as Step, so pressing Step Over repeatedly walks
through main without descending into the methods it calls.
- Step Out executes the rest of the current method and stops in the
calling method, at the instruction after the call. The return is animated.
In a recursive method, Step Out leaves only the current call. In
main,
which has no caller, the button is disabled.
A breakpoint, a warning or an error stops both earlier, and so does Stop.
The memory model
As in Chapter 12, there is a single stack. A method call does not copy its
arguments: the arguments on top of the caller's operand stack become the first
local variables of the new frame, and LV points to the first of them. A
frame with L local variables (.limit locals L) looks like this:
LV+L+2 … operand stack of the method
LV+L+1 saved LV: the value of LV in the calling method
LV+L return address: where the caller continues
LV+n … LV+L-1 the other local variables (shown as ? until assigned)
LV+0 … LV+n-1 the n arguments
The two grey cells are the bookkeeping cells. ireturn removes the frame,
puts the result where the arguments were, and restores LV from the saved
value. Cells above SP keep their old values (shown faintly), since pop
only decrements SP.
What is simulated
- int arithmetic with the semantics of Java: overflow wraps around,
idiv
rounds towards 0, the remainder of irem has the sign of the dividend, the
shifts use the lowest 5 bits of the shift distance:
iadd isub imul idiv irem ineg iand ior ixor ishl ishr iushr iinc
- constants and the stack:
iconst_<n> bipush sipush ldc aconst_null dup pop swap nop
- local variables:
iload istore aload astore (also iload_<n> etc.)
- jumps:
goto if<cond> if_icmp<cond> if_acmpeq if_acmpne ifnull ifnonnull
- static methods of the same class:
invokestatic ireturn areturn return
- printing:
getstatic java.lang.System.out java.io.PrintStream (or err) and
invokevirtual java.io.PrintStream.print(…) / println(…) with an int,
char, boolean, java.lang.String or java.lang.Object argument.
Other instructions (objects, arrays, long, float, double, …) are
reported when the program is loaded and stop the simulation when they are
reached. Constructors (<init>) are not executed: the simulation starts with
main.
Checks
The simulator stops with an explanation when something happens that the JVM
would reject or that ends the program:
- the operand stack grows beyond
.limit stack (a warning the first time this
happens in a method, and Play stops; the cells beyond the limit are always
marked red),
- a local variable beyond
.limit locals is used, or one is read before a
value has been stored in it,
- an instruction finds too few values or values of the wrong type on the stack,
- a method runs past its last instruction,
- a division by zero (
java.lang.ArithmeticException).
When the program ends, the output area lists for every method the largest
number of values its operand stack has held, compared with .limit stack.
Development
pip install -e . # in a clone of the repository
python3 -m unittest discover -s tests
A release on GitHub with the tag v + version (e.g. v0.1, the version is
__version__ in pytunium/__init__.py) publishes the package on PyPI (see
.github/workflows/publish.yml).
The machine (pytunium/machine.py) does not depend on the user interface:
every call of Machine.step() returns an Event that describes which cells
were taken from the stack, which cells were written and the formula of the
working area. The user interface (pytunium/gui.py) animates these events.
pytunium/
program.py parser for pytunia programs (Java notation and descriptors)
loader.py checks the syntax with pytunia, then parses
machine.py the simulated JVM; one Event per instruction
values.py values in the cells, Java int arithmetic
gui.py Tkinter user interface
examples/ Sum.jas, Hello.jas, Primes.jas
tests/ unit tests (no window is opened)
License
BSD 3-Clause, see LICENSE.txt.