Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b0a906a
feat(compiler): scaffold Hyperbee.ExpressionCompiler library with stu…
bfarmer67 Mar 2, 2026
e8f8f89
feat(compiler): scaffold Hyperbee.Expressions.Compiler library with s…
bfarmer67 Mar 2, 2026
aa88b8d
test(compiler): add CompilerType enum, ExpressionVerifier, and binary…
bfarmer67 Mar 2, 2026
4047e1b
test(compiler): seed FEC issue regression tests
bfarmer67 Mar 2, 2026
4527b24
bench(compiler): add compilation and execution benchmarks
bfarmer67 Mar 2, 2026
e5b2537
chore(compiler): remove incorrectly named Hyperbee.ExpressionCompiler…
bfarmer67 Mar 2, 2026
f102a6c
chore(compiler): restore version.json accidentally removed in cleanup
bfarmer67 Mar 2, 2026
9d8aac8
feat(compiler): add IR instruction set (IROp, IRInstruction, IRBuilder)
bfarmer67 Mar 2, 2026
4055f6d
feat(compiler): add ExpressionLowerer for Phase 1 expression types
bfarmer67 Mar 2, 2026
3e71e02
feat(compiler): add ILEmissionPass for IR-to-CIL generation
bfarmer67 Mar 2, 2026
5328bac
feat(compiler): wire HyperbeeCompiler pipeline and pass all Phase 1 t…
bfarmer67 Mar 2, 2026
2dee0bc
feat(compiler): add Phase 2 exception handling with try/catch/finally…
bfarmer67 Mar 2, 2026
ab77eef
refactor(compiler): extract Compile pipeline into discrete SRP methods
bfarmer67 Mar 2, 2026
3fadcfb
refactor(compiler): rename RunPasses to TransformIR
bfarmer67 Mar 2, 2026
5146a1b
feat(compiler): add Phase 3 closure support with captured variables a…
bfarmer67 Mar 2, 2026
d54b1fd
feat(compiler): add Phase 4 completeness — loop, switch, array, colle…
bfarmer67 Mar 2, 2026
d8ba82f
feat(compiler): add Phase 5 optimization — PeepholePass, allocation r…
bfarmer67 Mar 2, 2026
7cf3794
fix(compiler): remove CompileWithFallback masking, fix 30 IR lowering…
bfarmer67 Mar 2, 2026
eb12e8b
fix(compiler): Pattern 8 native, switch result-local, increment/decre…
bfarmer67 Mar 2, 2026
3405e3b
feat(compiler): Phase 6 — nullable lifted ops, exception filters, Run…
bfarmer67 Mar 2, 2026
d5b5b7a
fix(compiler): document FEC crash on Not(bool?) as known failure
bfarmer67 Mar 2, 2026
42d8113
feat(compiler): Phase 7 — test suite expansion, FEC policy enforcemen…
bfarmer67 Mar 2, 2026
950cda2
feat(compiler): Phase 8 — expand test suite to 2,426 instances, fix c…
bfarmer67 Mar 2, 2026
8372931
fix(compiler): fix null Nullable<T> constant crash, update benchmarks…
bfarmer67 Mar 2, 2026
d275776
revert(expressions): remove MoveNextCompiler hook, restore clean Asyn…
bfarmer67 Mar 3, 2026
4f11aca
feat(compiler): add CompileToInstanceMethod + state-machine pattern t…
bfarmer67 Mar 3, 2026
487bd6f
feat(compiler): Milestone 1 — pluggable coroutine builder interfaces
bfarmer67 Mar 3, 2026
62794de
refactor(compiler): rename AsyncInterpreterTaskBuilder to AsyncTaskMe…
bfarmer67 Mar 3, 2026
76b504e
feat(compiler): Milestone 2 — HEC compiles all BlockAsync patterns, t…
bfarmer67 Mar 3, 2026
ee0b016
feat(compiler): Milestone 3 — ambient CoroutineBuilderContext, IExpre…
bfarmer67 Mar 3, 2026
0535860
refactor(tests): move FEC Pattern 28 to FecKnownIssues, remove always…
bfarmer67 Mar 4, 2026
9d1f52d
fix(compiler): Phase 5 IL optimization — eliminate merge-point locals…
bfarmer67 Mar 4, 2026
81d73fb
refactor(compiler): remove dead code, fix Exchange perf, add benchmar…
bfarmer67 Mar 4, 2026
28a3590
docs: comprehensive just-the-docs documentation structure
bfarmer67 Mar 4, 2026
c25fc09
docs: replace non-ASCII characters with ASCII equivalents
bfarmer67 Mar 4, 2026
65aae51
Remove non-ascii chars from docs
bfarmer67 Mar 4, 2026
5261bfa
feat: Add IR opcodes for fused comparison branches and CIL switch
bfarmer67 Mar 4, 2026
79e047e
feat: Support new IR opcodes in IR pipeline diagnostics and validation
bfarmer67 Mar 4, 2026
29e0c49
feat: Implement CIL emission for fused branch and switch instructions
bfarmer67 Mar 4, 2026
a553416
perf: Implement peephole optimizations for fused comparison branches …
bfarmer67 Mar 4, 2026
28df0d6
fix: Optimize redundant IROp.Leave instructions in exception blocks
bfarmer67 Mar 4, 2026
74bd828
feat: Enhance SwitchExpression lowering with CIL jump table optimization
bfarmer67 Mar 4, 2026
3f31d49
test: Introduce ILComparisonTool for compiler output diagnostics
bfarmer67 Mar 4, 2026
51cb723
docs: Update README with minor formatting and text improvements
bfarmer67 Mar 4, 2026
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
3,487 changes: 3,487 additions & 0 deletions Hyperbee.ExpressionCompiler.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Hyperbee.Expressions.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
</Folder>
<Folder Name="/Solution Tests/">
<Project Path="test/Hyperbee.Expressions.Benchmark/Hyperbee.Expressions.Benchmark.csproj" />
<Project Path="test/Hyperbee.Expressions.Compiler.Benchmarks/Hyperbee.Expressions.Compiler.Benchmarks.csproj" />
<Project Path="test/Hyperbee.Expressions.Compiler.IssueTests/Hyperbee.Expressions.Compiler.IssueTests.csproj" />
<Project Path="test/Hyperbee.Expressions.Compiler.Tests/Hyperbee.Expressions.Compiler.Tests.csproj" />
<Project Path="test/Hyperbee.Expressions.Tests/Hyperbee.Expressions.Tests.csproj" />
</Folder>
<Project Path="src/Hyperbee.Expressions.Compiler/Hyperbee.Expressions.Compiler.csproj" />
<Project Path="src/Hyperbee.Expressions.Lab/Hyperbee.Expressions.Lab.csproj" />
<Project Path="src/Hyperbee.Expressions/Hyperbee.Expressions.csproj" />
</Solution>
23 changes: 19 additions & 4 deletions docs/_config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
title: Hyperbee.Expressions
description: Documentation for Hyperbee Expressions.
title: Hyperbee Expressions
description: Documentation for Hyperbee Expressions — async, yield, loop, and compiler extensions for .NET expression trees.
remote_theme: pmarsceill/just-the-docs
baseurl: "/hyperbee.expressions/"
url: "https://stillpoint-software.github.io"

# Optional configuration
search_enabled: true
aux_links:
"GitHub Repository":
- "//github.com/Stillpoint-Software/hyperbee.expressions"

footer_content: |
<div>
<span>&copy; <span id="copyright-year"></span> <a href='https://www.stillpointsoftware.net/'>Stillpoint Software</a>.</span>
<script>
document.getElementById("copyright-year").textContent = new Date().getFullYear();
</script>
</div>

search_enabled: true

nav_external_links:
- title: Stillpoint Software
url: https://www.stillpointsoftware.net/
opens_in_new_tab: true
3 changes: 3 additions & 0 deletions docs/_includes/nav_footer_custom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<footer class="site-footer">
Hyperbee Expressions Docs
</footer>
194 changes: 194 additions & 0 deletions docs/compiler/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
---
layout: default
title: API Reference
parent: Compiler
nav_order: 2
---

# API Reference

---

## HyperbeeCompiler

`HyperbeeCompiler` is a static class -- the primary entry point for all compilation operations.

```csharp
using Hyperbee.Expressions.Compiler;
```

### Compile

```csharp
static TDelegate Compile<TDelegate>( Expression<TDelegate> lambda, CompilerDiagnostics? diagnostics = null )
where TDelegate : Delegate

static Delegate Compile( LambdaExpression lambda, CompilerDiagnostics? diagnostics = null )
```

Compiles the expression tree and returns a delegate. Throws on unsupported patterns.

```csharp
var fn = HyperbeeCompiler.Compile<Func<int, int>>(
x => Expression.Add( x, Expression.Constant( 1 ) )
);
Console.WriteLine( fn( 41 ) ); // 42
```

### TryCompile

```csharp
static TDelegate? TryCompile<TDelegate>( Expression<TDelegate> lambda ) where TDelegate : Delegate
static Delegate? TryCompile( LambdaExpression lambda )
```

Compiles and returns `null` on failure instead of throwing.

```csharp
var fn = HyperbeeCompiler.TryCompile( lambda );
if ( fn is null )
Console.WriteLine( "Compilation failed" );
```

### CompileWithFallback

```csharp
static TDelegate CompileWithFallback<TDelegate>( Expression<TDelegate> lambda ) where TDelegate : Delegate
static Delegate CompileWithFallback( LambdaExpression lambda )
```

Attempts HEC compilation; falls back to `lambda.Compile()` (System compiler) on failure.
Use during migration when some expressions may not yet be supported.

```csharp
var fn = HyperbeeCompiler.CompileWithFallback( lambda );
```

### UseAsDefault / ClearDefault

```csharp
static ICoroutineDelegateBuilder? UseAsDefault()
static ICoroutineDelegateBuilder? ClearDefault()
```

Sets or clears HEC as the process-wide default builder for `AsyncBlockExpression` reductions.
Call at application startup to make HEC compile all `BlockAsync` state machines, even when the
outer lambda is compiled by another compiler.

```csharp
// In Program.cs or AssemblyInitialize
HyperbeeCompiler.UseAsDefault();

// In test teardown
HyperbeeCompiler.ClearDefault();
```

### CompileToMethod

```csharp
static void CompileToMethod( LambdaExpression lambda, MethodBuilder method )
static bool TryCompileToMethod( LambdaExpression lambda, MethodBuilder method )
```

Emits the expression tree directly into a `MethodBuilder`. The method must be `static` and its
parameter signature must match the lambda.

Non-embeddable constants (object references, delegates, nested lambdas) are not permitted --
all constants must be embeddable IL values (primitives, `Type` tokens, `null`).

```csharp
var typeBuilder = moduleBuilder.DefineType( "MyType" );
var methodBuilder = typeBuilder.DefineMethod(
"Add",
MethodAttributes.Public | MethodAttributes.Static,
typeof(int), [typeof(int), typeof(int)]
);

var a = Parameter( typeof(int), "a" );
var b = Parameter( typeof(int), "b" );
var lambda = Lambda( Add( a, b ), a, b );

HyperbeeCompiler.CompileToMethod( lambda, methodBuilder );

var type = typeBuilder.CreateType();
var method = type.GetMethod("Add")!;
Console.WriteLine( method.Invoke( null, [10, 32] ) ); // 42
```

### CompileToInstanceMethod

```csharp
static void CompileToInstanceMethod( LambdaExpression lambda, MethodBuilder method )
static bool TryCompileToInstanceMethod( LambdaExpression lambda, MethodBuilder method )
```

Like `CompileToMethod` but without the static-method requirement. For instance methods on value
types (e.g., `IAsyncStateMachine.MoveNext()`), the lambda's first parameter maps to IL `arg.0`
(the implicit `this` managed pointer).

---

## HyperbeeExpressionCompiler

`HyperbeeExpressionCompiler` implements `IExpressionCompiler` as a DI-friendly singleton wrapper
around `HyperbeeCompiler`.

```csharp
public sealed class HyperbeeExpressionCompiler : IExpressionCompiler
{
public static readonly IExpressionCompiler Instance;

public Delegate Compile( LambdaExpression lambda );
public TDelegate Compile<TDelegate>( Expression<TDelegate> lambda ) where TDelegate : Delegate;
public Delegate? TryCompile( LambdaExpression lambda );
public TDelegate? TryCompile<TDelegate>( Expression<TDelegate> lambda ) where TDelegate : Delegate;

public static ICoroutineDelegateBuilder? UseAsDefault();
}
```

### DI Registration

```csharp
// Register HEC as the IExpressionCompiler implementation
services.AddSingleton<IExpressionCompiler>( HyperbeeExpressionCompiler.Instance );
```

### With UseAsDefault

`HyperbeeExpressionCompiler.UseAsDefault()` is a convenience passthrough to
`HyperbeeCompiler.UseAsDefault()`. Call it at startup to make HEC compile all `BlockAsync`
state machines, even in expressions compiled by the System compiler.

```csharp
// In Program.cs
HyperbeeExpressionCompiler.UseAsDefault();
```

---

## HyperbeeCompilerExtensions

Extension methods for `LambdaExpression`:

```csharp
// In namespace Hyperbee.Expressions.Compiler
public static TDelegate CompileHyperbee<TDelegate>( this Expression<TDelegate> lambda )
where TDelegate : Delegate
```

```csharp
using Hyperbee.Expressions.Compiler;

var fn = lambda.CompileHyperbee();
```

---

## Notes

- All `HyperbeeCompiler` methods are thread-safe -- there is no shared mutable state.
- `CompileToMethod` and `CompileToInstanceMethod` do not support closures. Use `Compile()` for
expressions with captured variables or non-embeddable constants.
- See [Diagnostics](diagnostics.md) for `CompilerDiagnostics` and IR capture.
- See [Performance](performance.md) for benchmark comparisons.
54 changes: 54 additions & 0 deletions docs/compiler/compiler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
layout: default
title: Compiler
has_children: true
nav_order: 4
---

# Compiler

`Hyperbee.Expressions.Compiler` is a high-performance, IR-based compiler for .NET expression trees.
It is a drop-in replacement for `Expression.Compile()` that emits IL directly, bypassing the System
expression interpreter.

---

## Installation

```
dotnet add package Hyperbee.Expressions.Compiler
```

## Quick Start

```csharp
using Hyperbee.Expressions.Compiler;

// Direct replacement for lambda.Compile()
var fn = HyperbeeCompiler.Compile( lambda );

// Or use the IExpressionCompiler interface for DI
services.AddSingleton<IExpressionCompiler>( HyperbeeExpressionCompiler.Instance );
```

---

## Topics

| Topic | Description |
|-------|-------------|
| [Overview](overview.md) | Architecture, compilation pipeline, optimization passes |
| [API Reference](api.md) | `HyperbeeCompiler`, `HyperbeeExpressionCompiler`, `CompileToMethod` |
| [Diagnostics](diagnostics.md) | IR capture, `CompilerDiagnostics`, `IRFormatter` |
| [Performance](performance.md) | Benchmarks vs System compiler and FastExpressionCompiler |

---

## Highlights

- **9-34x faster** compilation than the System compiler
- **1.16-1.54x** of FastExpressionCompiler (FEC) compilation time
- **Up to 50% fewer** allocations than the System compiler
- Supports all expression patterns -- including those FEC does not support
- Fully compatible with `AsyncBlockExpression` state machines via ambient context
- `IExpressionCompiler` interface for DI-friendly injection
Loading