Hi everyone,
I wanted to share sccc, a self-hosting compiler and x86-64 toolchain written in C99 (GPLv3). These are baby steps toward writing my own completely self-hosting operating system.
The compiler makes a single pass over the source using a recursive descent parser that emits x86-64 machine code directly without an AST or IR, sharing its instruction encoder with a built-in AT&T-syntax assembler (supporting 16-bit, 32-bit, and 64-bit modes) and linker. It writes ELF relocatable objects, static and shared ELF executables, ELF32 boot code, flat binaries, raw PC BIOS disk images with a built-in boot sector that sets up E820 and long mode, and Multiboot 1 kernels. It also interprets GNU linker scripts, supports higher-half kernel addressing, interrupt handler attributes, x86 control register, MSR, and port I/O builtins, thread-local storage, and GCC-style extended inline assembly including goto labels. It compiles itself to identical second and third stage binaries, and builds xv6-x86_64 and Linux 5.4.302 alongside musl 1.2.5 and BusyBox 1.36.1, booting both to an interactive shell in QEMU.
The repository includes a set of bare-metal test kernels (IDT exceptions, PIT timer, PS/2 keyboard, E820 page frame allocator, higher-half kernel, and context switching) in baremetal/ that can be built and run directly in QEMU or Bochs, as well as automated test suites comparing assembly output against gas and linker script output against GNU ld.
Technical notes covering the compiler internals, instruction encoder, linker-script evaluator, and Kbuild/OS build details are also included in the notes/ directory.
Feedback, bug reports, or questions about the implementation are very welcome as I start moving on to the OS side of things.
Repository: https://github.com/gaidardzhiev/sccc