-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update AST read/written variable finders.
- Loading branch information
Showing
3 changed files
with
57 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,41 @@ | ||
using System.Collections.Generic; | ||
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 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