Skip to content

· Mehdi Berel · Internals · 3 min read

Zero external tools: how Fid emits a native .exe directly

No assembler, no linker, no C compiler, no LLVM. A tour of the pipeline that turns Fid source into a valid Windows executable, byte by byte.

Most modern languages compile by handing an intermediate representation to LLVM, then handing LLVM’s output to an external assembler and linker. That pipeline is powerful, but it is also why a “hello world” build can pull in gigabytes of toolchain and why compile times balloon.

Fid takes a different path: the compiler produces a runnable native executable by itself. Run emit on a source file and the only thing that touches disk is a finished .exe.

The pipeline

Source (.fd) → Lexer → Parser → TypeChecker → Monomorphizer → Lowerer → SSA IR → PE Emitter → .exe

Every stage is hand-written:

  • Lexer & parser: a hand-rolled tokenizer and recursive-descent parser. No parser generators.
  • Type checker: validates the full AST and reports all errors at once instead of stopping at the first.
  • Monomorphizer: analyzes every generic call site and generates specialized copies, so generics cost nothing at runtime.
  • Lowerer: turns the checked AST into a typed SSA (Static Single Assignment) IR, where every value has a single definition and control flow becomes basic blocks with explicit terminators.

The PE emitter

The last stage is where it gets unusual. Instead of printing assembly text for someone else’s assembler, the PE emitter encodes x86-64 instructions directly to machine-code bytes (REX prefixes, ModR/M and SIB bytes, displacements, immediates) and then constructs a valid Windows Portable Executable around them:

  • DOS header, PE header, and section headers for .text and .rdata
  • An import directory and Import Address Table that pull GetStdHandle, WriteFile, and ExitProcess from kernel32.dll via RIP-relative addressing
  • Runtime support (integer-to-string conversion, printing, panic) generated inline as machine code, so there is no libc and no C runtime anywhere in the binary
  • Full Windows x64 ABI compliance: RCX/RDX/R8/R9 argument registers, 32-byte shadow space, 16-byte stack alignment

Control flow uses label-based relocation with deferred resolution; string literals live in .rdata; every IR value gets a stack slot at a fixed frame offset.

Why bother?

Three reasons.

Speed. Skipping LLVM, an external assembler, and an external linker removes entire process boundaries and serialization steps from every build. Sub-second builds for small projects aren’t an optimization target. They’re the default consequence of the architecture.

Control. Because the IR is ours, it can carry Fid semantics (regions, effects, capabilities) all the way down. A generic backend can’t optimize around information it never sees.

Independence. A compiler with zero external dependencies is a compiler that installs instantly, cross-compiles predictably, and can eventually be rewritten in Fid itself. Self-hosting is the endgame of the roadmap, and this architecture is what makes it feasible.

Two additional backends, MASM assembly and portable C, exist alongside the PE emitter for verification and portability during the bootstrap phase, plus an IR interpreter for instant iteration. But the direct emitter is the point: the shortest possible line from your source code to the machine.

  • compiler
  • toolchain
  • internals
Share:
MB

Mehdi Berel

Engineering notes from the Fid compiler. Everything measured on the real toolchain: nothing on this blog is aspirational.

Back to Blog

Related Posts

View All Posts »

4 min read

The compiler that emits itself, bit-for-bit

fid emit compiles the entire 104K-line self-hosted compiler straight to a Windows executable (no assembler, no linker, no C compiler) and the output is byte-identical to the emitter that produced it. Here is why that matters and how it got there.