| name | bellard-minimalist-wizardry |
| description | Write systems software in the style of Fabrice Bellard, creator of QEMU, FFmpeg, TinyCC, and JSLinux. Emphasizes extreme minimalism, solo mastery of complex domains, and making the impossible seem simple. Use when building emulators, compilers, codecs, or any system where performance and code density matter. |
Fabrice Bellard Style Guide
Overview
Fabrice Bellard created QEMU (the universal machine emulator), FFmpeg (the multimedia framework), TinyCC (a tiny C compiler), JSLinux (Linux in a browser), and computed record digits of pi. He's arguably the most prolific solo systems programmer alive, repeatedly delivering production-quality systems that others would staff entire teams to build.
Core Philosophy
"The best code is code you don't write."
"Understand the problem deeply before writing a single line."
"Constraints breed creativity."
Bellard believes in deep understanding over brute force—knowing the domain so well that elegant, minimal solutions become obvious.
Design Principles
-
Radical Minimalism: Every line must earn its place.
-
Deep Domain Mastery: Understand the spec better than anyone.
-
Solo Excellence: One person can build world-class systems.
-
Performance Through Simplicity: Simple code is often fastest.
When Writing Systems Code
Always
- Read the specification thoroughly before coding
- Start with the simplest possible implementation
- Profile before optimizing
- Keep the entire system in your head
- Write portable C that compiles anywhere
- Release working code, then iterate
Never
- Over-engineer the first version
- Use frameworks when libraries suffice
- Add abstraction without clear benefit
- Write code you don't fully understand
- Optimize without measurements
- Let code grow without pruning
Prefer
- C for systems code (maximum control)
- Tables over code (data-driven design)
- Integer math over floating point
- Static allocation over dynamic
- Single-file implementations when possible
- Bitwise operations for flags and state
Code Patterns
TinyCC: A C Compiler in 100KB
typedef struct {
int type;
int value;
char *str;
} Token;
void parse_declaration(void) {
int type = parse_type();
while (tok != ';') {
char *name = parse_declarator(type);
if (tok == '=') {
next();
parse_initializer();
}
if (tok == ',') next();
}
expect(';');
}
void gen_op(int op) {
switch (op) {
case '+':
o(0x01); o(0xc0 | (REG_EAX << 3) | vtop->r);
break;
case '*':
o(0x0f); o(0xaf); o(0xc0 | (REG_EAX << 3) | vtop->r);
break;
}
}
QEMU: Dynamic Binary Translation
typedef struct TranslationBlock {
target_ulong pc;
void *tc_ptr;
struct TranslationBlock *next;
} TranslationBlock;
void tcg_gen_add_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2) {
tcg_gen_op3_i32(INDEX_op_add_i32, ret, arg1, arg2);
}
static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb) {
tcg_qemu_tb_exec(cpu->env_ptr, tb->tc_ptr);
}
static inline void *tlb_lookup(CPUState *cpu, target_ulong addr) {
int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
if (cpu->tlb_table[index].addr_read == (addr & TARGET_PAGE_MASK)) {
return (void *)(addr + cpu->tlb_table[index].addend);
}
return tlb_fill_slowpath(cpu, addr);
}
FFmpeg: Multimedia Swiss Army Knife
typedef struct AVCodec {
const char *name;
enum AVMediaType type;
enum AVCodecID id;
int (*init)(AVCodecContext *);
int (*encode)(AVCodecContext *, AVPacket *, const AVFrame *, int *);
int (*decode)(AVCodecContext *, AVFrame *, int *, AVPacket *);
int (*close)(AVCodecContext *);
} AVCodec;
static AVCodec *first_avcodec = NULL;
void avcodec_register(AVCodec *codec) {
codec->next = first_avcodec;
first_avcodec = codec;
}
void ff_h264_idct_add_c(uint8_t *dst, int16_t *block, int stride) {
for (int i = 0; i < 4; i++) {
int a = block[0] + block[2];
b = block[] - block[];
c = (block[] >> ) - block[];
d = block[] + (block[] >> );
}
}
;
Table-Driven Design
static const uint8_t coeff_token_vlc[4][17][4] = {
{{1, 1, 0, 0}, {6, 5, 0, 1}, {8, 7, 1, 1}, ...},
{{2, 2, 0, 0}, {6, 5, 0, 1}, {6, 5, 1, 1}, ...},
};
typedef enum { S_START, S_NUMBER, S_STRING, S_END } State;
static const State transitions[S_END][256] = {
[S_START] = {
['0' ... '9'] = S_NUMBER,
['"'] = S_STRING,
[' '] = S_START,
},
};
State next_state(State current, char c) {
return transitions[current][(unsigned char)c];
}
Integer Math for Precision
#define FRAC_BITS 16
#define FRAC_ONE (1 << FRAC_BITS)
int resample(int16_t *out, int16_t *in, int in_len, int ratio) {
int64_t pos = 0;
int out_idx = 0;
while (pos < ((int64_t)in_len << FRAC_BITS)) {
int idx = pos >> FRAC_BITS;
int frac = pos & (FRAC_ONE - 1);
out[out_idx++] = (in[idx] * (FRAC_ONE - frac) +
in[idx + 1] * frac) >> FRAC_BITS;
pos += ratio;
}
return out_idx;
}
Single-File Mastery
typedef struct {
uint8_t *mem;
uint32_t regs[8];
uint32_t eip;
uint32_t eflags;
struct {
uint8_t data[16];
int read_pos, write_pos;
} serial;
} PCState;
static void exec_instruction(PCState *s) {
uint8_t op = fetch_byte(s);
switch (op) {
case 0x89:
modrm = fetch_byte(s);
decode_modrm(s, modrm, &addr, ®);
write_mem32(s, addr, s->regs[reg]);
break;
case 0x8b:
modrm = fetch_byte(s);
decode_modrm(s, modrm, &addr, ®);
s->regs[reg] = read_mem32(s, addr);
break;
}
}
Portable Performance
void *page_alloc(size_t size);
void page_protect(void *addr, size_t size, int flags);
#ifdef _WIN32
void *page_alloc(size_t size) {
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
}
#else
void *page_alloc(size_t size) {
return mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
}
#endif
static int has_sse2 = 0;
void init_cpu_features(void) {
#if defined(__i386__) || defined(__x86_64__)
uint32_t eax, ebx, ecx, edx;
__cpuid(1, eax, ebx, ecx, edx);
has_sse2 = (edx >> 26) & 1;
#endif
}
Project Scope Philosophy
Bellard's Project Characteristics
══════════════════════════════════════════════════════════════
Project Lines of Code What It Does
────────────────────────────────────────────────────────────
TinyCC ~30,000 Full C99 compiler + linker
QEMU ~500,000* Universal machine emulator
FFmpeg ~1,000,000* All multimedia formats
JSLinux ~10,000 PC emulator in JavaScript
*Grew over time; Bellard's initial versions much smaller
Key insight: Start minimal, prove the concept works,
then expand based on real needs.
Mental Model
Bellard approaches problems by asking:
- What's the essence? Strip away everything non-essential
- What do the specs actually say? Read them completely
- What's the minimal viable implementation? Start there
- Where's the hot path? Optimize only what matters
- Can one person maintain this? Complexity is the enemy
Signature Bellard Moves
- TinyCC's speed: Compile fast enough to use as a scripting language
- QEMU's TCG: Dynamic translation that's portable across hosts
- FFmpeg's codec zoo: Support everything through uniform interfaces
- JSLinux: Boot Linux in a browser, because why not
- Pi computation: World records with algorithms, not hardware
- Self-hosting compilers: TCC compiles itself
- Single-file deployments: Reduce dependencies to zero