Convert Figma logo to code with AI

rui314 logo8cc

A Small C Compiler

6,127
740
6,127
41

Top Related Projects

A readable lisp in less than 1k lines of C

14,102

Mold: A Modern Linker 🦠

Quick Overview

8cc is a small, self-hosting C compiler written in C. It supports a subset of the C99 standard and can generate x86-64 assembly code. The project aims to be a simple and educational implementation of a C compiler.

Pros

  • Simplicity: The codebase is relatively small and easy to understand, making it a good learning resource for those interested in compiler design.
  • Self-hosting: The compiler can compile itself, demonstrating its ability to generate valid and functional code.
  • Portability: The compiler can generate code for the x86-64 architecture, making it widely applicable.
  • Educational: The project is designed to be an educational resource, providing insights into the inner workings of a C compiler.

Cons

  • Limited Functionality: The compiler only supports a subset of the C99 standard, which may limit its usefulness for real-world projects.
  • Lack of Optimization: The generated code may not be as efficient as that produced by more mature and optimized compilers.
  • Lack of Tooling: The project does not provide a comprehensive set of tools, such as a debugger or a package manager, which can make it less user-friendly.
  • Maintenance: As a relatively small project, the long-term maintenance and development of the compiler may be a concern.

Code Examples

Here are a few code examples from the 8cc project:

  1. Lexer:
int lex(void) {
  while (1) {
    switch (c) {
    case ' ': case '\t': case '\n': case '\r':
      c = getc(stdin);
      continue;
    case 'a': case 'b': case 'c': case 'd': case 'e':
    case 'f': case 'g': case 'h': case 'i': case 'j':
    case 'k': case 'l': case 'm': case 'n': case 'o':
    case 'p': case 'q': case 'r': case 's': case 't':
    case 'u': case 'v': case 'w': case 'x': case 'y':
    case 'z':
    case 'A': case 'B': case 'C': case 'D': case 'E':
    case 'F': case 'G': case 'H': case 'I': case 'J':
    case 'K': case 'L': case 'M': case 'N': case 'O':
    case 'P': case 'Q': case 'R': case 'S': case 'T':
    case 'U': case 'V': case 'W': case 'X': case 'Y':
    case 'Z':
    case '_':
      return read_ident();
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      return read_number();
    case '+': c = getc(stdin); return '+';
    case '-': c = getc(stdin); return '-';
    case '*': c = getc(stdin); return '*';
    case '/': c = getc(stdin); return '/';
    case '(': c = getc(stdin); return '(';
    case ')': c = getc(stdin); return ')';
    case '{': c = getc(stdin); return '{';
    case '}': c = getc(stdin); return '}';
    case ';': c = getc(stdin); return ';';
    case '=': c = getc(stdin); return '=';
    case EOF: return 0;
    default:
      error("unexpected character: %c", c);
    }
  }
}

This code is the lexer function, which is responsible for tokenizing the input C code.

  1. Parser:
Node *stmt() {
  Node *node;
  switch (token) {
  case '{':
    return compound_stmt();
  case IF:
    return if_stmt();
  case WHILE:

Competitor Comparisons

A readable lisp in less than 1k lines of C

Pros of minilisp

  • Simpler and more focused project compared to 8cc, with a smaller codebase.
  • Provides a good starting point for learning about Lisp-style programming languages.
  • Includes a basic REPL (Read-Eval-Print Loop) for interactive development.

Cons of minilisp

  • Limited functionality compared to 8cc, which is a more feature-rich C compiler.
  • Lacks the extensive documentation and community support available for 8cc.
  • May not be suitable for more complex programming tasks that require a full-featured compiler.

Code Comparison

8cc

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "usage: %s <file>\n", argv[0]);
        return 1;
    }
    FILE *f = fopen(argv[1], "r");
    if (!f) {
        perror(argv[1]);
        return 1;
    }
    // ...
}

minilisp

(define (main args)
  (if (not (= (length args) 2))
      (begin
        (print "usage: minilisp <file>")
        1)
      (let ((f (open-input-file (list-ref args 1))))
        (if (not f)
            (begin
              (print "error: could not open file")
              1)
            (begin
              (repl f)
              0)))))
14,102

Mold: A Modern Linker 🦠

Pros of mold

  • mold is a faster and more efficient linker compared to 8cc, which can significantly improve build times for large projects.
  • mold supports a wider range of object file formats, including COFF, ELF, and Mach-O, making it more versatile than 8cc.
  • mold has a more modular design, allowing for easier customization and integration with other build tools.

Cons of mold

  • mold has a smaller user base and community compared to 8cc, which may make it more challenging to find support and resources.
  • mold is a more recent project, so it may have fewer features and less mature functionality compared to the more established 8cc.
  • mold may have compatibility issues with certain build systems or project configurations that are well-supported by 8cc.

Code Comparison

Here's a brief comparison of the code structure between 8cc and mold:

8cc (main.c):

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "usage: %s [options] file...\n", argv[0]);
        return 1;
    }
    // ...
    return 0;
}

mold (main.cpp):

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s [options] file...\n", argv[0]);
        return 1;
    }
    // ...
    return 0;
}

As you can see, the overall structure of the main function is similar between the two projects, but mold is written in C++ while 8cc is written in C.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

8cc C Compiler

Note: 8cc is no longer an active project. The successor is chibicc.

8cc is a compiler for the C programming language. It's intended to support all C11 language features while keeping the code as small and simple as possible.

The compiler is able to compile itself. You can see its code both as an implementation of the C language and as an example of what this compiler is able to compile.

8cc's source code is carefully written to be as concise and easy-to-read as possible, so that the source code becomes good study material to learn about various techniques used in compilers. You may find the lexer, the preprocessor and the parser are already useful to learn how C source code is processed at each stage.

It's not an optimizing compiler. Generated code is usually 2x or more slower than GCC. I plan to implement a reasonable level of optimization in the future.

8cc supports x86-64 Linux only. I have no plan to make it portable until I fix all known miscompilations and implement an optimization pass. As of 2015, I'm using Ubuntu 14 as my development platform. It should work on other x86-64 Linux distributions though.

Note: Do not have high expectations on this compiler. If you try to compile a program other than the compiler itself, there's a good chance to see compile errors or miscompilations. This is basically a one-man project, and I have spent only a few months of my spare time so far.

Build

Run make to build:

make

8cc comes with unit tests. To run the tests, give "test" as an argument:

make test

The following target builds 8cc three times to verify that stage1 compiler can build stage2, and stage2 can build stage3. It then compares stage2 and stage3 binaries byte-by-byte to verify that we reach a fixed point.

make fulltest

Author

Rui Ueyama rui314@gmail.com

Links for C compiler development

Besides popular books about compiler, such as the Dragon Book, I found the following books/documents are very useful to develop a C compiler. Note that the standard draft versions are very close to the ratified versions. You can practically use them as the standard documents.

NPM DownloadsLast 30 Days