|
BFC-Compiler
A C23 Brainfuck compiler
|
Project README · Backend guide · CLI reference · API documentation
bfc is a C23 Brainfuck compiler. The current pipeline reads a Brainfuck source file, validates and tokenizes it, builds and optimizes a nested IR, selects a target-specific backend, and emits assembly text.
Current implemented backends:
The target parser also recognizes Linux and Windows triples, but those targets remain unavailable until their backend objects are implemented and registered.
A future final stage can assemble and link the emitted assembly:
In the current implementation, -S writes the generated assembly file. The assemble-and-link stage is not yet part of the compiler.
src/bfc.c coordinates the pipeline:
Cleanup attributes release allocated compiler objects when main() exits.
Owns command-line parsing and help output.
Current options:
bfc_args_t stores borrowed pointers into argv; it does not own the input, output, or target strings.
Owns source-file loading and line lookup.
bfc_program_t contains the copied path, source buffer, file size, and line count.
The token layer defines Brainfuck tokens and token streams.
The lexer:
Validates matching [ and ] tokens.
The current IR builder does not consume the resulting table; the module is presently a separate validation pass. It can be removed later if bracket validation moves into bfc_ir_create().
Defines the persistent intermediate representation.
Current operations:
Loops contain nested bfc_ir_block_t objects.
Defines target architecture and operating-system types.
Explicit --target selection should drive cross-target code generation; host detection is only the default.
Owns generic assembly generation:
Target-specific syntax must remain in backend modules.
Each backend defines one immutable bfc_backend_t object and private emitter functions.
Current backend files:
Backends own assembly syntax, symbol naming, ABI rules, register use, immediate lowering, sections, and branch syntax.
Defines error codes, error constructors, and diagnostics.
bfc_error_t is nodiscard, so returned errors must be checked. Token-bearing errors can print file, line, column, source text, and a caret.
| Object | Created by | Destroyed by |
|---|---|---|
| bfc_program_t | bfc_program_create() | bfc_program_destroy() |
| bfc_token_stream_t | bfc_lex() | bfc_token_stream_destroy() |
| jump table | bfc_parse_jump_table() | bfc_jump_table_destroy() |
| bfc_ir_block_t | bfc_ir_create() | bfc_ir_destroy() |
| bfc_asm_t | bfc_codegen() | bfc_asm_destroy() |
bfc_asm_t stores:
The buffer remains null-terminated; length excludes the terminator.
bfc_codegen_emitf() is the shared formatted-emission helper.
Generic codegen owns loop structure:
This keeps loop structure architecture-independent.