aython

Documentation4 min read

Reading the assembly

Three panes per program: your Python, the assembly the frontend emits, and the runtime helpers it calls, all generated from the repository at build time.

Pick a program. On the left is the Python, in the middle the assembly the real frontend and code generator emit for it, and on the right a helper from runtime.s. Select a line of Python and the assembly its statement produced is marked; select a line of assembly and the statement that produced it is marked. Every call into the runtime is a link: select it and the helper opens on the right.

Corpus programs with their generated assembly and runtime helpers, all produced from the repository when this site was built. Accent marks the lines a statement emitted itself; the fainter mark is code from statements nested inside it, such as a loop body or a function body. Underlined names are calls and jumps: runtime helpers open on the right, labels move to that label. The Python and assembly panes take Up and Down; Enter follows a call.

What to try #

  • Arithmetic and assignment (pass_arith.py): Each x = … evaluates into temporaries, parks the result, releases the old value, then stores. Click line 2 and follow int_mul.
  • Aliasing and reference counts (pass_alias.py): b = a is one aython_incref: two slots, one list. y += 1 builds a new int and leaves x alone.
  • if / else (pass_if_basic.py): Every test goes through aython_not and one compare against False; the branch is one conditional jump.
  • while loop (pass_while_sum.py): A while is a label at the top, the same test as if, and a jmp back. i <= 100 uses the raw-bound helper: no box for 100.
  • for over a range (pass_for_range.py): A for parks the iterable, keeps a raw counter, and re-reads the length every pass.
  • A recursive function (pass_fn_fib.py): The call passes n in rdi with the count in r10d; fn_fib checks the count, adopts the argument, and every return jumps to one shared epilogue.
  • Lists (pass_list_mut.py): Item stores and appends: the list keeps its own reference to each element.
  • Strings (pass_str_basic.py): Literals are pooled bytes in .rodata, copied into a fresh box by str_new.
  • Dicts (pass_dict_basic.py): A display is dict_new plus one obj_setitem per pair.
  • A class (pass_class_basic.py): No class object exists: Counter() is a fresh dict plus a call to fn_Counter____init__, and c.bump(5) calls fn_Counter__bump with c as the first argument.
  • Comprehensions (pass_comprehension.py): Each comprehension is an empty result plus one inline loop per for, appending at the innermost level.
  • Imports (pass_import_from.py): The helper module’s statements run where the import stood, with names prefixed inc_import_from_helper__. Both files are on the left.
  • try / except (pass_try_basic.py): exc_push records where to land; a failure anywhere inside jumps to the handler label with the kind id in exc_kind, and the handlers compare it.
  • Exceptions across calls (pass_try_fn.py): The ZeroDivisionError from int_floordiv inside g unwinds straight past fn_g’s frame to the handler in main_main.
  • finally (pass_try_finally_bare.py): The finally body is emitted twice: once on the normal path and once on the unwind path, which then re-raises the saved kind.

How the mapping is made #

The code generator writes a # line N: comment at the start of every statement, which is enough to read by eye but not to tell where a statement’s code ends: an if contains its branches, a function contains its body, and a finally body is emitted more than once. So the site does not parse comments. At build time, docsite/figures/_spans.py wraps four functions of codegen.py for one run: _emit_stmt (every statement), _emit_function (a whole function), and _emit_def_defaults and _emit_class_defaults (what a def or class statement does where it stands). Each wrapper puts a marker line before and after the lines the real function returned. The markers are then removed, and the result must equal, byte for byte, the assembly aython run would assemble; if it does not, the build stops. Nothing in src/aython is changed, and no mapping is shown for any assembly other than the real one.

Each assembly line belongs to the innermost statement whose emitter produced it. Lines that belong to none are the file’s directives, main_main’s own prologue and epilogue, and the data sections. For an imported module, the frontend’s parser is wrapped the same way to remember which file each statement came from, so the helper module’s lines are marked in its own file.

How to read a helper #

Every helper borrows its arguments and returns a new owned reference, or nothing. A few rules cover most of what the listings do:

You see It means Explained in
mov rax, [rbp-8] then call aython_incref Reading a variable: the expression takes its own reference Emitting assembly
mov [rbp-40], rax after a call Parking a value in memory before the next call can overwrite rax Emitting assembly
call aython_not, then cmp rax, rcx against aython_false Every branch test, whatever its type Emitting assembly
mov r10d, N before call fn_... The argument count, checked by the callee Emitting assembly
jne abort_type, jmp abort_runtime A failure: unwind to a handler, or print aython: <Kind> and exit 1 Emitting assembly
call exc_push … call exc_pop A try body Emitting assembly
fn_Counter__bump, inc_import_from_helper__double A method or an imported function, flattened by the frontend The frontend

The object layout behind every [rax + 8] and [rax + 16] is in Objects in memory, and the helpers one by one in The runtime.