$ show capstone-engine/capstone
Capstone is a multi-architecture disassembly engine written in C, derived from LLVM's MC disassembler. It supports over 20 architectures including x86, ARM, AArch64, MIPS, PowerPC, RISC-V, and more. The core library provides a stable C API (cs_open, cs_disasm, cs_option, etc.) and includes per-architecture backends with mapping, disassembler, and instruction printer modules. The architecture is modular: each architecture has a Module.c that registers callbacks into a common cs_struct. The primary data flow is: user provides raw bytes and arch/mode, the engine selects the appropriate disassembler, decodes instructions into MCInst, then prints via instruction printer, optionally filling cs_detail with operand/register/group information. The repository also includes a CLI tool (cstool), extensive test suites (MC regression tests, cstest YAML-based detail tests), fuzzing harnesses, and an automated LLVM sync pipeline (auto-sync) that translates LLVM C++ disassembler code to C using tree-sitter patches. The codebase is well-structured with clean abstractions for operand models (MCInst/MCOperand), string output (SStream), and architecture-specific mapping. The auto-sync tooling is a notable engineering achievement, enabling systematic translation of LLVM backend code to C. Language bindings for Python, Java, OCaml, and others demonstrate the library's embeddability. The test infrastructure is comprehensive, covering both basic disassembly and detailed operand/register analysis. External dependencies are minimal for the core library (pure C), but the auto-sync tooling depends on Python, tree-sitter, clang-format, and lit. Overall, Capstone is a mature, actively maintained disassembly framework with high craftsmanship and reusable components.
The LLM identified 5 files worth highlighting for reuse:
Base class for tree-sitter-based patches that translate LLVM C++ disassembler code to C. Each patch subclass defines a search pattern, capture name, and replacement logic. This pattern is reusable for any project that needs to systematically transform C++ ASTs to C, and demonstrates non-obvious skill in AST manipulation and automated code translation.
Implements the MCInst and MCOperand data structures, including operand creation, writeback detection, soft-fail marking, and tied-operand handling. This is a self-contained C implementation of LLVM's MCInst model, reusable for any disassembler or instruction representation project. Demonstrates careful handling of edge cases like writeback registers and conditional soft-fail.
Provides a string buffer abstraction with markup support for instruction printing. Includes functions for concatenating strings, numbers, and markup tags (imm, reg, mem, target). This is a reusable output utility that decouples instruction printers from output formatting, and could be extracted for any project needing structured text output with annotations.
Header with macros for comparing expected vs actual instruction detail fields (tbool, ints, enums, register names, bit flags). Used by the cstest framework to validate cs_detail output against YAML test cases. This pattern is reusable for any project that needs to compare complex nested structures in tests, and demonstrates a clean approach to test assertion generation.
Main Python binding module that wraps the Capstone C library using ctypes. Provides a clean object-oriented API (Cs class) with methods for disassembly, option setting, and register access. The pattern of using ctypes to wrap a C library with per-architecture detail structures is reusable for any project needing Python bindings for a C library with complex output types.