-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from Washi1337/feature/new-ast-builder
New AST builder.
- Loading branch information
Showing
109 changed files
with
4,434 additions
and
2,073 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Echo.Code; | ||
|
||
namespace Echo.Ast.Analysis; | ||
|
||
/// <summary> | ||
/// Provides a wrapper around a <see cref="IPurityClassifier{TInstruction}"/> that is able to classify statements | ||
/// and expressions with <typeparamref name="TInstruction"/> instructions by purity. | ||
/// </summary> | ||
/// <typeparam name="TInstruction">The type of instructions the statements store.</typeparam> | ||
public class AstPurityClassifier<TInstruction> : IPurityClassifier<Statement<TInstruction>> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the <see cref="AstPurityClassifier{TInstruction}"/> class. | ||
/// </summary> | ||
/// <param name="baseClassifier">The base classifier to use for classifying individual instructions in the AST.</param> | ||
public AstPurityClassifier(IPurityClassifier<TInstruction> baseClassifier) | ||
{ | ||
BaseClassifier = baseClassifier; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the base classifier to use for classifying individual instructions in the AST. | ||
/// </summary> | ||
public IPurityClassifier<TInstruction> BaseClassifier | ||
{ | ||
get; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean IsPure(in Statement<TInstruction> instruction) | ||
{ | ||
return instruction.Accept(AstPurityVisitor<TInstruction>.Instance, BaseClassifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Echo.Code; | ||
|
||
namespace Echo.Ast.Analysis; | ||
|
||
/// <summary> | ||
/// Provides a mechanism for traversing an AST and determining its purity. | ||
/// </summary> | ||
/// <typeparam name="TInstruction">The type of instructions to store in each expression.</typeparam> | ||
public class AstPurityVisitor<TInstruction> : IAstNodeVisitor<TInstruction, IPurityClassifier<TInstruction>, Trilean> | ||
{ | ||
/// <summary> | ||
/// Gets the singleton instance of the <see cref="AstPurityVisitor{TInstruction}"/> class. | ||
/// </summary> | ||
public static AstPurityVisitor<TInstruction> Instance | ||
{ | ||
get; | ||
} = new(); | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(CompilationUnit<TInstruction> unit, IPurityClassifier<TInstruction> state) | ||
{ | ||
return unit.Root.Accept(this, state); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(AssignmentStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) => false; | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(ExpressionStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) | ||
{ | ||
return statement.Expression.Accept(this, state); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(PhiStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) => false; | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(BlockStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) | ||
{ | ||
var result = Trilean.True; | ||
|
||
for (int i = 0; i < statement.Statements.Count && result != Trilean.False; i++) | ||
result &= statement.Statements[i].Accept(this, state); | ||
|
||
return result; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(ExceptionHandlerStatement<TInstruction> statement, IPurityClassifier<TInstruction> state) | ||
{ | ||
var result = statement.ProtectedBlock.Accept(this, state); | ||
|
||
for (int i = 0; i < statement.Handlers.Count && result != Trilean.False; i++) | ||
result &= statement.Handlers[i].Accept(this, state); | ||
|
||
return result; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(HandlerClause<TInstruction> clause, IPurityClassifier<TInstruction> state) | ||
{ | ||
var result = Trilean.True; | ||
|
||
if (clause.Prologue is not null) | ||
result &= clause.Prologue.Accept(this, state); | ||
|
||
if (result.ToBooleanOrFalse()) | ||
result &= clause.Contents.Accept(this, state); | ||
|
||
if (clause.Epilogue is not null && result.ToBooleanOrFalse()) | ||
result &= clause.Epilogue.Accept(this, state); | ||
|
||
return result; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(InstructionExpression<TInstruction> expression, IPurityClassifier<TInstruction> state) | ||
{ | ||
var result = state.IsPure(expression.Instruction); | ||
|
||
for (int i = 0; i < expression.Arguments.Count && result != Trilean.False; i++) | ||
result &= expression.Arguments[i].Accept(this, state); | ||
|
||
return result; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Trilean Visit(VariableExpression<TInstruction> expression, IPurityClassifier<TInstruction> state) => true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,49 @@ | ||
using Echo.Code; | ||
using System; | ||
using Echo.Code; | ||
|
||
namespace Echo.Ast.Analysis | ||
{ | ||
internal sealed class FlowControlDeterminer<TInstruction> | ||
: IAstNodeVisitor<TInstruction, object, InstructionFlowControl> | ||
: IAstNodeVisitor<TInstruction, object?, InstructionFlowControl> | ||
{ | ||
private readonly IArchitecture<TInstruction> _isa; | ||
|
||
internal FlowControlDeterminer(IArchitecture<TInstruction> isa) => | ||
_isa = isa; | ||
|
||
public InstructionFlowControl Visit(AssignmentStatement<TInstruction> assignmentStatement, object state) => | ||
assignmentStatement.Expression.Accept(this, state); | ||
public InstructionFlowControl Visit(CompilationUnit<TInstruction> unit, object? state) => | ||
throw new NotSupportedException(); | ||
|
||
public InstructionFlowControl Visit(ExpressionStatement<TInstruction> expressionStatement, object state) => | ||
expressionStatement.Expression.Accept(this, state); | ||
public InstructionFlowControl Visit(AssignmentStatement<TInstruction> statement, object? state) => | ||
statement.Expression.Accept(this, state); | ||
|
||
public InstructionFlowControl Visit(PhiStatement<TInstruction> phiStatement, object state) => | ||
public InstructionFlowControl Visit(ExpressionStatement<TInstruction> statement, object? state) => | ||
statement.Expression.Accept(this, state); | ||
|
||
public InstructionFlowControl Visit(PhiStatement<TInstruction> statement, object? state) => | ||
InstructionFlowControl.Fallthrough; | ||
|
||
public InstructionFlowControl Visit(InstructionExpression<TInstruction> instructionExpression, object state) => | ||
_isa.GetFlowControl(instructionExpression.Instruction); | ||
public InstructionFlowControl Visit(BlockStatement<TInstruction> statement, object? state) | ||
{ | ||
return statement.Statements.Count > 0 | ||
? statement.Statements[statement.Statements.Count - 1].Accept(this, state) | ||
: InstructionFlowControl.Fallthrough; | ||
} | ||
|
||
public InstructionFlowControl Visit(ExceptionHandlerStatement<TInstruction> statement, object? state) | ||
{ | ||
return statement.ProtectedBlock.Accept(this, state); | ||
} | ||
|
||
public InstructionFlowControl Visit(HandlerClause<TInstruction> clause, object? state) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public InstructionFlowControl Visit(InstructionExpression<TInstruction> expression, object? state) => | ||
_isa.GetFlowControl(expression.Instruction); | ||
|
||
public InstructionFlowControl Visit(VariableExpression<TInstruction> variableExpression, object state) => | ||
public InstructionFlowControl Visit(VariableExpression<TInstruction> expression, object? state) => | ||
InstructionFlowControl.Fallthrough; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using Echo.Code; | ||
|
||
namespace Echo.Ast.Analysis | ||
{ | ||
internal sealed class ReadVariableFinder<TInstruction> : AstNodeListener<TInstruction> | ||
{ | ||
private readonly IArchitecture<TInstruction> _architecture; | ||
|
||
public ReadVariableFinder(IArchitecture<TInstruction> architecture) | ||
{ | ||
_architecture = architecture; | ||
} | ||
|
||
internal HashSet<IVariable> Variables { get; } = new(); | ||
|
||
public override void ExitVariableExpression(VariableExpression<TInstruction> expression) | ||
{ | ||
base.ExitVariableExpression(expression); | ||
Variables.Add(expression.Variable); | ||
} | ||
|
||
public override void ExitInstructionExpression(InstructionExpression<TInstruction> expression) | ||
{ | ||
int count = _architecture.GetReadVariablesCount(expression.Instruction); | ||
if (count == 0) | ||
return; | ||
|
||
var variables = ArrayPool<IVariable>.Shared.Rent(count); | ||
|
||
int actualCount = _architecture.GetReadVariables(expression.Instruction, variables); | ||
for (int i = 0; i < actualCount; i++) | ||
Variables.Add(variables[i]); | ||
|
||
ArrayPool<IVariable>.Shared.Return(variables); | ||
|
||
base.ExitInstructionExpression(expression); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using Echo.Code; | ||
|
||
namespace Echo.Ast.Analysis | ||
{ | ||
internal sealed class WrittenVariableFinder<TInstruction> : AstNodeListener<TInstruction> | ||
{ | ||
private readonly IArchitecture<TInstruction> _architecture; | ||
|
||
public WrittenVariableFinder(IArchitecture<TInstruction> architecture) | ||
{ | ||
_architecture = architecture; | ||
} | ||
|
||
internal HashSet<IVariable> Variables { get; } = new(); | ||
|
||
public override void ExitAssignmentStatement(AssignmentStatement<TInstruction> statement) | ||
{ | ||
base.ExitAssignmentStatement(statement); | ||
for (int i = 0; i < statement.Variables.Count; i++) | ||
Variables.Add(statement.Variables[i]); | ||
} | ||
|
||
public override void ExitPhiStatement(PhiStatement<TInstruction> phiStatement) | ||
{ | ||
base.ExitPhiStatement(phiStatement); | ||
Variables.Add(phiStatement.Representative); | ||
} | ||
|
||
public override void ExitInstructionExpression(InstructionExpression<TInstruction> expression) | ||
{ | ||
int count = _architecture.GetWrittenVariablesCount(expression.Instruction); | ||
if (count == 0) | ||
return; | ||
|
||
var variables = ArrayPool<IVariable>.Shared.Rent(count); | ||
|
||
int actualCount = _architecture.GetWrittenVariables(expression.Instruction, variables); | ||
for (int i = 0; i < actualCount; i++) | ||
Variables.Add(variables[i]); | ||
|
||
ArrayPool<IVariable>.Shared.Return(variables); | ||
|
||
base.ExitInstructionExpression(expression); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.