Home /Examples/lib/examples/decompiler/LEARNING-NOTES.md

LEARNING-NOTES.md

lib/examples/decompiler/LEARNING-NOTES.md · 52 line(s) · markdown

Decompiler examples — what to learn here

These examples teach how to read a compiled artifact safely: inspect its structure before trusting it, parse its section map, and disassemble its bytecode into readable mnemonics. No real artifact is ever decoded — the inputs are small in-memory stand-ins — so every run is deterministic and side-effect free.

Read them in order.

The three cases

LevelFileThe one new idea
BasicBASIC-artifact-inspection.sleelaA safe inspection workflow: identify → verify digest → inspect → preserve → compare.
ModerateMODERATE-section-table.sleelaParse a header + section table (name, offset, size) and sanity-check it.
AdvancedADVANCED-disassemble-stream.sleelaWalk a bytecode stream, decode each opcode, and read its operands.

What we want you to take away

  1. Structure before code. Every real format — ELF, Java class files, SLeeLa artifacts — is a header followed by a map of sections. A decompiler parses that map first and never assumes it is well-formed: the moderate example checks that no section overlaps the header and that the bytes add up.
  2. Disassembly is a decode loop. The advanced example is the heart of any disassembler: read one opcode, look up its mnemonic, and advance the program counter by 1 or 2 depending on whether the instruction carries an operand. Getting operand lengths right is what keeps the stream aligned.
  3. Inspect without modifying. Step 4 of the basic workflow — preserve the original artifact unchanged — is a safety rule. You verify a digest before you look, and you compare your output to a known-good fixture afterward.

Practical notes for SLeeLa 1.7

Run them

sleela run lib/examples/decompiler/MODERATE-section-table.sleela
sleela run lib/examples/decompiler/ADVANCED-disassemble-stream.sleela

Output is deterministic: the section table accounts for 800 bytes total; the disassembler prints PUSH 6 / PUSH 7 / MUL / PRINT / HALT and decodes 5 instructions.