Command Palette

Search for a command to run...

Назад к скиллам
скилл № 02809

compiler-design

Use when the user asks about writing a compiler or interpreter, lexers, parsers, ASTs, grammars, type checkers, SSA, LLVM IR, MLIR, optimization passes or…

БесплатноБез исполняемых скриптов
j4flmaoот j4flmao★ 47 звёзд на GitHub

Установить compiler-design в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Об этом скилле

Compiler Design

Purpose

Build a language implementation that is correct first and fast second. This skill gives decision rules for each phase — which parsing technique, what the AST and IR should look like, which optimisations matter, and when to lean on LLVM/MLIR/Cranelift instead of writing a backend.

Agent Protocol

Trigger

"compiler", "lexer", "parser", "AST", "grammar", "LLVM IR", "SSA", "optimization pass", "codegen", "register allocation", "interpreter", "MLIR", "type checker".

Input Context

  • Source language (existing or new), size of the grammar, target (native, WASM, bytecode VM, JVM)
  • Implementation language and whether LLVM/Cranelift/MLIR may be used
  • Priorities: compile speed, runtime speed, error messages, incremental/IDE support

Output Artifact

Phase design, grammar, data structures, pass code or LLVM IR examples. No file unless requested.

Response Format

Pipeline: {lex -> parse -> resolve/typecheck -> IR -> passes -> codegen}
Choice per phase: {technique + reason}
Code/IR: {snippet}
Tests: {golden files, fuzzing, differential testing}

1. Pipeline

source → lexer (tokens) → parser (CST/AST) → name resolution + type checking (typed AST / HIR)
       → lowering to IR (SSA) → optimisation passes → instruction selection
       → register allocation + scheduling → object code / bytecode

Keep each stage's data structure explicit; most bugs come from phases sharing mutable state.

2. Lexing

  • Tokens = kind + span (byte offsets) + optional interned value. Spans enable good diagnostics.
  • Hand-written lexers (a match on the current char) are simple and fast; generators (re2c, flex, logos in Rust) build DFAs from regular expressions.
  • Handle maximal munch, keywords vs identifiers (look up after scanning an identifier), nested comments (needs a counter, not a regex), and string escapes.

3. Parsing

Technique Strength Use
Recursive descent (LL) readable, great errors, easy recovery most production compilers (GCC, Clang, Go, Rust, TypeScript use hand-written parsers)
Pratt / precedence climbing expressions with many precedence levels embedded inside recursive descent
LR/LALR (yacc/bison), GLR handles left recursion, grammar-checked when a formal grammar is the source of truth
PEG / packrat ordered choice, no ambiguity DSLs; watch memory and error quality
Parser combinators compact in functional languages small/medium languages

Pratt expression parser core:

def parse_expr(self, min_bp=0):
    lhs = self.parse_prefix()                  # literals, identifiers, unary ops, parens
    while True:
        op = self.peek()
        if op.kind not in INFIX: break
        l_bp, r_bp = INFIX[op.kind]            # e.g. '+': (10, 11) left-assoc, '^': (31, 30) right-assoc
        if l_bp < min_bp: break
        self.advance()
        rhs = self.parse_expr(r_bp)
        lhs = Binary(op, lhs, rhs)
    return lhs
  • Error recovery: synchronise on statement boundaries (;, }) and keep parsing to report several errors.
  • For IDEs, a lossless concrete syntax tree (Roslyn, rust-analyzer's rowan, tree-sitter) enables incremental reparsing and formatting.

4. Semantic Analysis

  • Name resolution builds scopes (stack of hash maps) and links uses to declarations.
  • Type checking: simple languages check bottom-up; ML-style languages use Hindley–Milner unification; modern languages often use bidirectional checking (infer + check modes).
  • Desugar after checking so errors refer to user code; keep spans on every node.

5. Intermediate Representation

  • SSA: every value assigned once; φ-nodes merge values at control-flow joins. Simplifies dataflow analyses (constant propagation, GVN, DCE).
  • Construct SSA with the dominance-frontier algorithm (Cytron et al.) or, from an AST directly, Braun et al.'s simple SSA construction. LLVM frontends usually emit allocas and let mem2reg/SROA promote them.

Example LLVM IR for int max(int a, int b) { return a > b ? a : b; }:

define i32 @max(i32 %a, i32 %b) {
entry:
  %cmp = icmp sgt i32 %a, %b
  br i1 %cmp, label %then, label %else
then:
  br label %end
else:
  br label %end
end:
  %r = phi i32 [ %a, %then ], [ %b, %else ]
  ret i32 %r
}

(After simplifycfg this becomes a single select.)

6. Optimisation Passes

  • Analysis passes compute facts (dominator tree, loop info, alias analysis, scalar evolution); transform passes rewrite IR (inlining, SROA, InstCombine, GVN, LICM, loop unrolling/vectorisation, DCE).
  • LLVM's new pass manager (default since LLVM 13; the legacy PM for the middle-end optimisation pipeline was removed in LLVM 17, while codegen still largely uses it) caches analyses and invalidates them based on the PreservedAnalyses each pass returns:
struct CountAdds : llvm::PassInfoMixin<CountAdds> {
  llvm::PreservedAnalyses run(llvm::Function &F, llvm::FunctionAnalysisManager &) {
    unsigne
Полный SKILL.md на GitHub

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

  • references/compiler-llvm.md
  • references/lsp-protocol.md

FAQ

Что делает скилл compiler-design?

Use when the user asks about writing a compiler or interpreter, lexers, parsers, ASTs, grammars, type checkers, SSA, LLVM IR, MLIR, optimization passes or register allocation. Produces compiler pipeline design and working front-end and IR code. Not for reading assembly output (use lang-assembly) or CPU microarchitecture (use cpu-architecture).

Как установить скилл compiler-design?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл compiler-design запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить compiler-design с