Allow LocalVariable to be assigned more than once (#2288)

* Allow `LocalVariable` to be assigned more than once

This allows us to write flow controls like loops and if-elses with
LocalVariables participating in phi nodes.

* Add `GetLocalNumber` to operand
This commit is contained in:
FICTURE7 2021-05-17 03:54:53 +04:00 committed by GitHub
parent 212e472c9f
commit c805542b29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 16 deletions

View file

@ -10,15 +10,17 @@ namespace ARMeilleure.Translation
private BasicBlock[] _postOrderBlocks;
private int[] _postOrderMap;
public int LocalsCount { get; }
public BasicBlock Entry { get; }
public IntrusiveList<BasicBlock> Blocks { get; }
public BasicBlock[] PostOrderBlocks => _postOrderBlocks;
public int[] PostOrderMap => _postOrderMap;
public ControlFlowGraph(BasicBlock entry, IntrusiveList<BasicBlock> blocks)
public ControlFlowGraph(BasicBlock entry, IntrusiveList<BasicBlock> blocks, int localsCount)
{
Entry = entry;
Blocks = blocks;
LocalsCount = localsCount;
Update(removeUnreachableBlocks: true);
}

View file

@ -12,6 +12,8 @@ namespace ARMeilleure.Translation
{
class EmitterContext
{
private int _localsCount;
private readonly Dictionary<Operand, BasicBlock> _irLabels;
private readonly IntrusiveList<BasicBlock> _irBlocks;
@ -23,6 +25,8 @@ namespace ARMeilleure.Translation
public EmitterContext()
{
_localsCount = 0;
_irLabels = new Dictionary<Operand, BasicBlock>();
_irBlocks = new IntrusiveList<BasicBlock>();
@ -30,6 +34,15 @@ namespace ARMeilleure.Translation
_nextBlockFreq = BasicBlockFrequency.Default;
}
public Operand AllocateLocal(OperandType type)
{
Operand local = Local(type);
local.NumberLocal(++_localsCount);
return local;
}
public Operand Add(Operand op1, Operand op2)
{
return Add(Instruction.Add, Local(op1.Type), op1, op2);
@ -223,9 +236,10 @@ namespace ARMeilleure.Translation
public Operand Copy(Operand dest, Operand op1)
{
if (dest.Kind != OperandKind.Register)
if (dest.Kind != OperandKind.Register &&
(dest.Kind != OperandKind.LocalVariable || dest.GetLocalNumber() == 0))
{
throw new ArgumentException($"Invalid dest operand kind \"{dest.Kind}\".");
throw new ArgumentException($"Destination operand must be a Register or a numbered LocalVariable.");
}
return Add(Instruction.Copy, dest, op1);
@ -670,7 +684,7 @@ namespace ARMeilleure.Translation
public ControlFlowGraph GetControlFlowGraph()
{
return new ControlFlowGraph(_irBlocks.First, _irBlocks);
return new ControlFlowGraph(_irBlocks.First, _irBlocks, _localsCount);
}
}
}

View file

@ -45,7 +45,7 @@ namespace ARMeilleure.Translation
public static void Construct(ControlFlowGraph cfg)
{
var globalDefs = new DefMap[cfg.Blocks.Count];
var localDefs = new Operand[RegisterConsts.TotalCount];
var localDefs = new Operand[cfg.LocalsCount + RegisterConsts.TotalCount];
var dfPhiBlocks = new Queue<BasicBlock>();
@ -264,6 +264,12 @@ namespace ARMeilleure.Translation
return true;
}
else if (operand is { Kind: OperandKind.LocalVariable } && operand.GetLocalNumber() > 0)
{
result = RegisterConsts.TotalCount + operand.GetLocalNumber() - 1;
return true;
}
result = -1;
@ -274,7 +280,7 @@ namespace ARMeilleure.Translation
{
if (!TryGetId(operand, out int key))
{
Debug.Fail("OperandKind must be Register.");
Debug.Fail("OperandKind must be Register or a numbered LocalVariable.");
}
return key;