How felipensp/vtcc works
Main Components
• Frontend/CLI Module: Represented by the vtcc executable and the main entry point (hello.c is used as a sample C source file). This includes the command-line interface that accepts input files and flags (e.g., -run, -h, etc.).
• Parser and Preprocessor: Although not pinpointed with a specific “lexer” file, several files like tccpp.v indicate preprocessing and parsing responsibilities.
• Code Generation Module: Files such as tccgen.v, tccasm.v, and x86_64-gen.v focus on converting the high-level representation into architecture-specific assembly.
• Backend Linking and Executable Generation: Components like x86_64-link.v and modules within src (tcctools.v, tccrun.v) manage linking and the final executable output.
• Debugging and Symbol Information: Files including tccdbg.v, and support for DWARF (dwarf.h.v) and ELF (elf.h.v) suggest components meant for debugging and handling executable metadata.
• Libraries and Runtime Support: The lib directory contains helper modules (e.g., libtcc.v, bt-log.v, bt-exe.v) and runtime libraries (libtcc1 and its internal files) that the compiler relies on during generation and execution.
• Build Scripts: The make.vsh file serves as the build script for compiling vtcc itself, so it is part of the infrastructure rather than the runtime architecture but should be noted.
Relationships and Interactions
• The CLI (vtcc executable) is the entry point that receives source code (e.g., hello.c) and passes it to the parser/preprocessor.
• After parsing, the resulting internal representation is fed into the code generation modules, which then may call architecture-specific generators (x86_64-gen.v) and perform assembly-level optimizations.
• Next, the linker/assembler component assembles and links the compiled modules and integrates runtime libraries from the “lib” folder (libtcc1) to produce the final executable.
• Debug and symbol generation modules (tccdbg.v, dwarf.h.v, elf.h.v) create debuggable metadata alongside the final binary.
• Optional routes exist for dynamic linking and external dependencies via tools and flags (for example, linking with libraries like pthread as indicated in the README).
Architectural Patterns and Technologies
• Although vtcc is a monolithic compiler, its source structure clearly separates concerns by module (frontend, backend, code generation, etc.), echoing ideas from classical compiler design.
• The design leverages the capabilities of V language and integrates external C libraries (notice vlang.c is provided, and there are interfaces to system libraries) to implement core functionalities.
• The use of external build scripts (make.vsh) and testing (tests directory) reinforces a structured development environment.