Skip to main content

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:

  1. Program: the assembler program. The next instruction is highlighted in yellow, the instruction executed before in blue.
  2. 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.
  3. 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.

Metadata

Release files for pytunium 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytunium 0.4
File Size Uploaded Provenance Actions
pytunium-0.4.tar.gz 46.1 kB With provenance Details for pytunium-0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytunium 0.4
File Interpreter ABI Platform Actions
pytunium-0.4-py3-none-any.whl Python 3 none any Details for pytunium-0.4-py3-none-any.whl

Total release size: 88.2 kB

Release files / pytunium-0.4.tar.gz

Download URL pytunium-0.4.tar.gz
Size 46.1 kB
Tags Source
SHA-256 checksum
How to use checksums
64ebb79052e46fbc25aeedbf46a0f2723da5bcc1d9fe0cf9545b30142b0f72f2
BLAKE2b-256 checksum
How to use checksums
5fba88cb1b00917bdda348cc49441f17f3ff6a241a091b66799b71d4dbd4542d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / pytunium-0.4-py3-none-any.whl

Download URL pytunium-0.4-py3-none-any.whl
Size 42.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2fe87919b4ffb72b80e77d848e5bfacb675f9f9a9e969855d5ba72714b4c2d5d
BLAKE2b-256 checksum
How to use checksums
d94cc1e50fa6b91b3ac8d24e4b70c88d6f43a36fbf339de86b89104df2b9b32a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for pytunium 0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytunium-0.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page