Skip to content

← All updates

session

Update 10: The direct PE emitter

The flagship backend landed: emit goes from source to a finished .exe with zero external tools.

  • Full x86-64 instruction encoder: REX prefixes, ModR/M + SIB bytes, displacement and immediate encoding, all 16 registers.
  • Complete PE file construction: DOS/PE headers, .text + .rdata sections, import directory, and an Import Address Table pulling GetStdHandle, WriteFile, and ExitProcess from kernel32.dll.
  • Print, panic, and allocation runtime generated inline as machine code, with no libc anywhere in the binary.
  • Deferred label relocation for jumps and calls; Windows x64 ABI-compliant stack frames.

Bugs worth remembering

Three subtle codegen bugs were found and fixed along the way:

  1. Swapped REX.R/REX.B bits in register-to-register moves, so any mov involving R8-R15 selected the wrong register, causing access violations.
  2. A 64-bit store clobbering adjacent stack data: zeroing a 4-byte variable wrote 8 bytes, silently corrupting the neighboring newline and digit bytes.
  3. An off-by-one in integer print length that dropped the trailing newline.

Output now matches the interpreter and MASM backends byte-for-byte, and exit codes propagate correctly.