Skip to content

Commit

Permalink
Update Stackifier.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 12, 2023
1 parent a9cabb2 commit 5885953
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Draco.Compiler/Internal/OptimizingIr/Stackifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ public Stackifier(IProcedure procedure)
// TODO: Doc
public ImmutableArray<IInstruction> Stackify(IBasicBlock basicBlock)
{
var result = ImmutableArray.CreateBuilder<IInstruction>();

var instr = basicBlock.LastInstruction;
while (instr is not null)
{
// This instruction has to have its registers saved
if (instr is IValueInstruction valueInstr) this.savedRegisters.Add(valueInstr.Target);
// Recover the longest tree backwards
this.RecoverTree(ref instr);
var (tree, _) = this.RecoverTree(ref instr);
// Add result
result.Add(tree);
// Step back
instr = instr.Prev;
}

result.Reverse();
return result.ToImmutable();
}

private (IOperand Tree, bool Stopped) RecoverTree(ref IInstruction instrIterator)
private (TreeInstruction Tree, bool Stopped) RecoverTree(ref IInstruction instrIterator)
{
var instr = instrIterator;
var children = ImmutableArray.CreateBuilder<IOperand>();
Expand Down Expand Up @@ -88,11 +93,7 @@ public ImmutableArray<IInstruction> Stackify(IBasicBlock basicBlock)
}

children.Reverse();
var tree = instr switch
{
IValueInstruction vi => new TreeInstruction(vi, children.ToImmutable()),
_ => throw new InvalidOperationException(),
};
var tree = new TreeInstruction(instr, children.ToImmutable());
return (tree, stopped);
}
}

0 comments on commit 5885953

Please sign in to comment.