Skip to content

Commit

Permalink
move optimizer to lowering visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
bfarmer67 committed Dec 5, 2024
1 parent fec2952 commit 5bba755
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
28 changes: 15 additions & 13 deletions src/Hyperbee.Expressions/AsyncBlockExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ public override Expression Reduce()

private LoweringInfo LoweringTransformer()
{
var visitor = new LoweringVisitor();

var loweringInfo = visitor.Transform(
Result.Type,
[.. Variables],
[.. Expressions],
ExternVariables != null ? [.. ExternVariables] : []
);

if ( loweringInfo.AwaitCount == 0 )
throw new InvalidOperationException( $"{nameof( AsyncBlockExpression )} must contain at least one {nameof( AwaitExpression )}." );

return loweringInfo;
try
{
var visitor = new LoweringVisitor();

return visitor.Transform(
Result.Type,
[.. Variables],
[.. Expressions],
ExternVariables != null ? [.. ExternVariables] : []
);
}
catch ( LoweringException ex )
{
throw new InvalidOperationException( $"Unable to lower {nameof(AsyncBlockExpression)}.", ex );
}
}

protected override Expression VisitChildren( ExpressionVisitor visitor )
Expand Down
6 changes: 6 additions & 0 deletions src/Hyperbee.Expressions/Transformation/LoweringException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Hyperbee.Expressions.Transformation;

internal class LoweringException : Exception
{
public LoweringException( string message ) : base( message ) { }
}
18 changes: 18 additions & 0 deletions src/Hyperbee.Expressions/Transformation/LoweringVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ internal class LoweringVisitor : ExpressionVisitor

public LoweringInfo Transform( Type resultType, ParameterExpression[] variables, Expression[] expressions, ParameterExpression[] externVariables )
{
ArgumentNullException.ThrowIfNull( expressions, nameof( expressions ) );
ArgumentOutOfRangeException.ThrowIfZero( expressions.Length, nameof( expressions ) );

_variableResolver = new VariableResolver( variables, _states );
_finalResultVariable = CreateFinalResultVariable( resultType, _variableResolver );

VisitExpressions( expressions );

StateOptimizer.Optimize( _states );

ThrowIfInvalid();

return new LoweringInfo
{
Scopes = _states.Scopes,
Expand All @@ -35,6 +42,17 @@ public LoweringInfo Transform( Type resultType, ParameterExpression[] variables,
ExternVariables = externVariables
};

// helpers

void ThrowIfInvalid()
{
if ( _states.Scopes[0].States.Count == 0 )
throw new LoweringException( $"Evaluation of the {nameof(expressions)} parameter resulted in empty states." );

if ( _awaitCount == 0 )
throw new LoweringException( $"The {nameof(expressions)} parameter must contain at least one awaitable." );
}

static ParameterExpression CreateFinalResultVariable( Type resultType, VariableResolver resolver )
{
var finalResultType = resultType == typeof( void )
Expand Down
12 changes: 2 additions & 10 deletions src/Hyperbee.Expressions/Transformation/StateMachineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ public StateMachineBuilder( ModuleBuilder moduleBuilder, string typeName )

public Expression CreateStateMachine( LoweringTransformer loweringTransformer, int id )
{
ArgumentNullException.ThrowIfNull( loweringTransformer, nameof(loweringTransformer) );

// Lower the async expression
//
var loweringInfo = loweringTransformer();

if ( loweringInfo.AwaitCount == 0 )
throw new InvalidOperationException( "The target of a lowering operation must contain at least one await." );

if ( loweringInfo.Scopes[0].States == null )
throw new InvalidOperationException( "States must be set before creating state machine." );

// Create the state-machine builder context
//
var context = new StateMachineContext { LoweringInfo = loweringInfo };
Expand Down Expand Up @@ -327,10 +323,6 @@ private static IEnumerable<Expression> CreateBody( FieldInfo[] fields, StateMach

var scopes = loweringInfo.Scopes;

// Optimize source nodes

StateMachineOptimizer.Optimize( loweringInfo );

// Create the body expressions

var firstScope = scopes[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace Hyperbee.Expressions.Transformation;

internal sealed class StateMachineOptimizer
internal sealed class StateOptimizer
{
internal static void Optimize( LoweringInfo source )
internal static void Optimize( StateContext states )
{
var references = new HashSet<LabelTarget>();
int stateOrder = 0;

var scopes = source.Scopes;
var scopes = states.Scopes;

foreach ( var scope in scopes )
{
Expand Down

0 comments on commit 5bba755

Please sign in to comment.