csharpc

DESIGN NOTES // ADRS // IL NOTES

HOW CSHARPC TURNS C# INTO ONE NATIVE FILE.

The same Markdown files that live in the repository's docs/ folder: the architecture, the runtime, each feature area, how everything is tested against real .NET, and every architecture decision.

csharpc documentation

Start with the top-level README for what csharpc is and how to install and use it, and with AGENTS.md for the rules of the repository (the one rule, the workflow, the test commands).

# Document Contents
00 Vision Goals, non-goals, what success looks like
01 Prior art What we borrow from clang, rustc, gollvm and the .NET AOT lineage; source→LLVM vs IL→LLVM
02 Architecture Pipeline, phases, Roslyn CFG as MIR, flow captures, linking, diagnostics
03 Lowering C# construct → LLVM IR, with real compiler output
04 Runtime csrt ABI and contents, exceptions and stack traces, the C / C# boundary, platforms, GC, finalizers
05 Testing Differential testing against dotnet run, the suites, CI
06 Roadmap Milestone status, the feature-support matrix, the standing goal
07 Spike log Dated journal of the first days and lessons learned
08 Drop-in toolchain .csproj input, dotnet build/run/publish integration, packaging, limits
09 Conformance The 195-program conformance corpus
10 Async async/await, iterators and async iterators as LLVM coroutines; the thread-pool scheduler
11 BCL value types decimal, TimeSpan/DateTime/Stopwatch, BigInteger, Type objects, Array helpers and sorting
12 Corelib The shadow corelib: BCL types written in C#, redirected from the real reference assemblies
13 Debugging -g, DWARF debug info, gdb/lldb pretty-printers
14 Threading Threads, the thread-safe GC (stop-the-world handshake), monitors, Interlocked, Parallel
15 Conformance corpus 2 Adversarial edge-case programs, wrong-output bugs found and fixed
16 Interop and I/O P/Invoke, LibraryImport, source generators, System.IO, sockets
17 Security Memory safety, exploit mitigations (--hardening), sanitizers and fuzzing, reproducible builds
18 Compatibility Real-world compatibility: Microsoft sample programs vs .NET and NativeAOT, the IL survey, ASP.NET Core
19 Globalization Cultures via ICU, culture selection, invariant mode, size
20 Status Where the project stands and how to pick the work up
21 Ownership Who owns what: the corelib mapped against “upstream owns it” (issue #191), with tools/il-first/ownership.sh
Contributing Reporting bugs, adding a test, CI, where things live

Architecture decision records

ADR Decision
0001 Roslyn is the C# front-end
0002 Bind the LLVM C API with [LibraryImport]
0003 Roslyn’s ControlFlowGraph is the mid-level IR
0004 Memory management: none → conservative GC → precise GC; the opt-in nursery
0005 Exceptions: LLVM EH with an Itanium-style personality
0006 Closed-world, whole-program compilation
0007 Bootstrap runtime in C, corelib in C# later
0008 Runtime library in C# (corelib), C only at the OS boundary
0009 Exploit mitigations on by default, CET opt-in
0010 IL front-end: IL as the method-body source for Roslyn’s metadata symbols; the program as IL
0011 WebAssembly: explicit exception propagation, compiler-spilled GC roots, one thread
0012 Stack traces: a landing pad per call site, names and lines from Roslyn’s own PDB
0013 Framework assemblies the program uses compiled from the shared framework’s IL (--framework-il)
0014 Compile-time scale: a ReadyToRun compiler, parallel parsing/binding/CFGs (-j), machine code per module partition, bounded memory
0015 The corelib compiled to IL once (cached) and lowered through the IL front-end (--corelib il, default)
0016 NativeAOT parity as the support target; csharpc as the AOT backend of a dotnet fork as the end state
0017 The runtime in three tiers: ordinary C#, compiler-checked “systems C#” (GC, EH, threads), a minimal native floor
0018 Member reflection over compile-time metadata, kept as NativeAOT keeps it
0019 dynamic bound at compile time with Roslyn, per run-time type, in a dispatch function per site
0020 Mostly-concurrent marking behind --gc-concurrent
0021 Finalizers, GC.SuppressFinalize, and the ObjectiveCMarshal reference tracking
0022 TLS and X.509: the corelib over OpenSSL, not the framework’s IL
0023 Framework IL first: hand-implement only the runtime-coupled core, compile the rest of the framework from its IL, per library by measurement (CSHARPC_IL_FIRST)
0024 Where the runtime boundary sits: our C runtime, or NativeAOT’s runtime with csharpc as the LLVM backend (a study; proposed)

The docs describe the code as it is. When a feature lands, update the feature matrix and the open bullets in 06-roadmap.md, and the document of the area it touches.

The site

These files are also published at jonathanperis.dev/csharpc, with a codebase map counted from the repository on every build. The site is plain Astro in this directory: it renders the Markdown files as they are (links between documents go to their pages, links into the repository go to GitHub), so a document is written once and reads well in both places. .github/workflows/pages.yml rebuilds and deploys it on every push to main. To work on it locally:

cd docs
bun install
bun run dev        # http://localhost:4321/

The layout is in src/layouts, the sidebar’s grouping in src/lib/nav.ts (a new document lands under “Reference” until it is placed in a group) and the codebase classification in src/lib/codebase.ts.