Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ ncc [OPTIONS] <FILENAMES>...
| `--no-iced` | Use text-based asm building instead of iced (deprecated) |
| `-o`, `--output <OUTPUT>` | Override output file location |
| `-h`, `--help` | Print help |
| `-V`, `--version` | Print version |

Note: `--lex`, `--parse`, `--validate`, `--codegen`, `--tacky`, `-S`, `--run`, `-c` are mutually exclusive options.

Expand All @@ -177,6 +176,32 @@ Note: `--lex`, `--parse`, `--validate`, `--codegen`, `--tacky`, `-S`, `--run`, `
| 20 | Parser error (syntax error) |
| 30 | Validation error (semantic error) |

## Architecture

NCC follows a classic multi-pass compiler pipeline:

```mermaid
flowchart LR
A[Source .c] --> B[Lexer]
B -->|Tokens| C[Parser]
C -->|Abstract AST| D[Validator]
D -->|Typechecked AST & Symbol Table| E[Tackifier]
E -->|TACKY IR| F[Codegen]
F -->|Assembly AST| G[Emitter]
G -->|Object File| H[Linker]
H --> I[Executable]
```

| Pass | Description |
|------|-------------|
| **Lexer** | Converts source text into tokens using regex patterns with maximal munch. Each token carries a span (file, line, column) for error reporting. |
| **Parser** | Recursive descent with precedence climbing. Builds an abstract syntax tree while handling operator precedence and associativity. |
| **Validator** | Two-pass semantic analysis: (1) resolves variables to unique names, labels loops/switches, validates gotos; (2) type checks, builds symbol table, evaluates constant expressions. |
| **Tackifier** | Lowers the AST to TACKY, a three-address code IR. Flattens nested expressions into sequences of simple operations and makes control flow explicit with jumps and labels. |
| **Codegen** | Converts TACKY to an x86-64 assembly AST. Assigns pseudo-registers to stack slots, fixes invalid instructions, and implements the System V AMD64 calling convention. |
| **Emitter** | Encodes instructions to machine code using [iced-x86](https://github.com/icedland/iced) and writes ELF/Mach-O object files via the [object](https://github.com/gimli-rs/object) crate. |
| **Linker** | Resolves external symbols and produces the final executable. Uses [wild](https://github.com/nicholasbishop/wild) on Linux; shells out to `ld` on macOS. |

## Language Grammar

The compiler currently implements a subset of C with the following grammar:
Expand Down
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ fn link_with_libwild(obj_files: &[String], out_file: &str, static_link: bool) {
fn main() {
let args = Args::parse();

// Only enable colors if stderr and stdout are TTYs (terminals)
use std::io::IsTerminal;
let is_tty = std::io::stderr().is_terminal() && std::io::stdout().is_terminal();
if is_tty {
colored::control::set_override(true);
// Enable TrueColor support for better color accuracy
// Safety: Setting COLORTERM is safe at program startup before any threads are spawned
unsafe {
std::env::set_var("COLORTERM", "truecolor");
}
}

// On macOS, always use external linker since libwild doesn't support it
let use_external_linker = args.external_linker || cfg!(target_os = "macos");

Expand Down Expand Up @@ -285,14 +297,14 @@ fn main() {

if args.validate {
println!("Program Valid");
println!("{}", ast.itf_string());
std::process::exit(0);
}

let (mut name_gen, symbols) = validated_result;
let tacky_ast = tacky::tackify_program(&ast, &mut name_gen, &symbols);

if args.tacky {
println!("{tacky_ast:?}");
println!("{}", tacky_ast.itf_string());
std::process::exit(0);
}
Expand Down
Loading