How rui314/8cc works
Main Components
• Compiler Front-End:
• Preprocessor: Look at files such as cpp.c and the include/ folder (with header definitions) and keyword.inc. This module is responsible for things like macro expansion and handling include directives.
• Lexer: The file lex.c implements the tokenizer, converting source code text into tokens.
• Parser: The parse.c module processes tokens from the lexer to build abstract representations (most likely an abstract syntax tree, AST).
• Compiler Back-End:
• Code Generator: gen.c is likely responsible for generating the target code based on the parsed structure (even if not heavily optimized).
• Other Supporting Modules:
• Utility Libraries: Modules like buffer.c, dict.c, map.c, vector.c, set.c provide data structures and helper routines that underpin the compiler’s internal data management.
• Error and Debug Handling: Files such as error.c and debug.c handle error reporting and debugging support.
• File and I/O Operations: file.c and path.c are used for file management and path resolution.
• Additional Compiler Stages: Other files like encoding.c, keyword.inc, and possibly those inside test/ (which contain unit tests verifying various parts of the language semantics and compiler behavior).
Relationships and Interactions
• The main function calls the preprocessor module (cpp.c) to handle macro expansion and other preparatory text transformations.
• The output of the preprocessor feeds into the lexer (lex.c), which tokenizes the source input.
• Tokens are then passed on to the parser (parse.c), which builds an internal representation, typically an AST.
• Once the AST is built, the code generator (gen.c) is invoked to generate the final target code.
• Throughout this process, utility modules (buffer, map, vector, etc.) are used by several components, so they should be shown as shared libraries or supporting modules.
• Testing modules (found in the test/ directory) can be represented separately, emphasizing that they interact with each of these internal components to ensure correctness.
Architectural Patterns and Key Design Principles
• Modular Design: The compiler is divided into distinct modules for preprocessing, lexical analysis, parsing, and code generation, which is typical for compiler design.
• Reusability and Conciseness: Utility libraries (e.g., for dynamic arrays, maps, and sets) are reused across multiple components.
• Testing: The rigorous set of unit tests emphasizes that each major component should have clearly defined inputs and outputs.
a. A “Main Application” block (representing main.c) at the top.
• Main Application → Preprocessor Module (cpp.c, keyword.inc, include/*)
• Preprocessor → Lexer Module (lex.c)
• Lexer → Parser Module (parse.c)
• Parser → Code Generator Module (gen.c)
g. Annotations for any nontrivial build targets noted in the Makefile (like the self-compilation stages mentioned in the README such as stage1 → stage2 → stage3).