How fruits-lab/vitaminc works
Project Type and Purpose
• The project is a compiler frontend aimed at demonstrating how a C compiler works. It handles compilation phases (lexer, parser, type checker, IR generator) and produces both QBE and LLVM IR.
• Key points from the README include its target IRs, support for C89 (with some C99 features), and the usage of LLVM’s compiler infrastructure alongside QBE.
File Structure
• Top-level files indicate configuration (.clang-format, .clang-tidy, Makefile, etc.), and the entry point is main.cpp.
• The presence of lexer.l and parser.y immediately indicates the implementation of the lexical analyzer (likely using Lex/Flex) and parser (using Yacc/Bison).
• The include/ and src/ directories contain header and source files respectively for the core components:
• AST: Files such as ast.hpp/ast.cpp and ast_dumper help in building and visualizing the AST.
• Type Checker: Files type_checker.hpp/type_checker.cpp.
• Symbol management, scope handling, and utility functions are also clearly separated.
• IR Generators: There are separate files and directories for llvm_ir_generator and qbe_ir_generator in both include and src.
• The test directory is well-organized into subdirectories for testing code generation and type checking, providing compiled examples (.c) and expected output (.exp) files. This shows that the project has strong testing infrastructure to validate the individual compilation stages.
Components
a. Main Components:
• Command-Line Interface & Driver Component: Represented by main.cpp; handles command-line options (using cxxopts), directing the input file to the subsequent phases.
• Lexical Analyzer: Based on lexer.l — responsible for tokenizing the input.
• Parser: Based on parser.y — builds the initial AST from tokens.
• Abstract Syntax Tree (AST): Comprising classes defined in ast.hpp/ast.cpp, and with support for dumping/debugging using ast_dumper.
• Type Checker: Responsible for semantic analysis; included in type_checker.hpp/type_checker.cpp.
• IR Generators: This branch splits into:
▪ LLVM IR Generator – uses LLVM libraries (see include/llvm_ir_generator.hpp/llvm_ir_generator.cpp and related utility code in src/llvm).
▪ QBE IR Generator – uses QBE-specific constructs (visible via include/qbe_ir_generator.hpp and src/qbe_ir_generator.cpp).
• Support Utility Modules: Include modules for symbol management (symbol.hpp/cpp), scope handling, and common utilities (util.hpp/cpp).
b. Relationships and Interactions:
• Data Flow: The input file is first processed by the Lexical Analyzer, then the Parser, which constructs the AST.
• The AST is then passed to the Type Checker for semantic validation.
• Depending on the command-line target option (via --target), the type-checked AST is handed to either the LLVM IR Generator or QBE IR Generator.
• Data flows sequentially from one compilation phase to the next, with each phase encapsulated as a distinct component.
• The testing framework (located in the test/ directory) validates code generation and type checking by comparing generated IR with expected outputs.
c. Architectural Patterns and Design Principles:
• Modularity and Separation of Concerns: Each compilation phase is separated into distinct classes and source files.
• Pipeline Architecture: The design forms a clear pipeline (Lexer → Parser → AST → Type Checker → IR Generation).
• Extensibility: The design is structured to allow additional IR targets or compiler phases to be incorporated with minimal coupling.
• Use of External Tools/Dependencies: Indicates integration with LLVM and QBE for IR generation, plus additional libraries such as cxxopts for argument parsing and fmt for formatting.
d. Relevant Technologies and Frameworks:
• C++17 for modern features.
• Lex/Flex and Yacc/Bison for lexical and syntactic analysis.
• LLVM and QBE for IR generation and further compilation.
• cxxopts and fmt libraries.
• GNU Make for building the project.
• Testing tools like turnt are used for snapshot tests in the test directory.