Skip to content

Commit

Permalink
Clean up control flow generation
Browse files Browse the repository at this point in the history
  • Loading branch information
atrexus committed Aug 13, 2024
1 parent 2224b82 commit ae96652
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/Unluau/IR/ProtoTypes/ControlFlow/BasicBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum BranchType
/// <summary>
/// This block never branches.
/// </summary>
Never
Never,
}

/// <summary>
Expand All @@ -31,7 +31,7 @@ public class BasicBlock : Node
/// <summary>
/// The kind of branch that is taken.
/// </summary>
public BranchType Branch { get; set; } = BranchType.Always;
public BranchType? Branch { get; set; }

/// <summary>
/// The list of instructions in the basic block.
Expand All @@ -45,6 +45,8 @@ public class BasicBlock : Node

public void AddEdge(BasicBlock target) => OutgoingEdges.Add(new Edge(this, target));

public bool IsDead => OutgoingEdges.Count == 0 && Branch == null;

/// <inheritdoc/>
public override void Accept(Visitor visitor)
{
Expand Down
29 changes: 13 additions & 16 deletions src/Unluau/IR/ProtoTypes/ControlFlow/Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void AddInstruction(Instruction instruction)

if (instruction.Context.Pc == pc + 1)
{
if (_currentBlock == oldBlock)
if (_currentBlock == oldBlock && _currentBlock.Instructions.Count > 0)
_currentBlock = new();

block.AddEdge(_currentBlock);
Expand All @@ -59,6 +59,9 @@ private void AddInstruction(Instruction instruction)

_currentBlock.Instructions.Add(instruction);

if (_currentBlock != oldBlock && oldBlock.IsDead)
oldBlock.AddEdge(_currentBlock);

switch (instruction.Code)
{
case OpCode.JumpX:
Expand All @@ -68,8 +71,6 @@ private void AddInstruction(Instruction instruction)
}
case OpCode.LoadB:
{
var branchType = instruction.C > 0 ? BranchType.Always : BranchType.Never;

AddEdge(instruction.C, instruction.Context.Pc, _currentBlock, BranchType.Always);
break;
}
Expand All @@ -79,21 +80,25 @@ private void AddInstruction(Instruction instruction)
AddEdge(instruction.D, instruction.Context.Pc, _currentBlock, BranchType.Always);
break;
}
case OpCode.ForNPrep:
{
AddEdge(0, instruction.Context.Pc, _currentBlock, BranchType.Always);
break;
}
case OpCode.Return:
{
AddEdge(0, instruction.Context.Pc, _currentBlock, BranchType.Never);
goto default;
break;
}
case OpCode.JumpXEqKNil:
case OpCode.JumpXEqKB:
case OpCode.JumpXEqKN:
case OpCode.JumpXEqKS:
{
var branchType = instruction.D > 0 ? BranchType.Can : BranchType.Never;

AddEdge(instruction.D == 1 ? 0 : instruction.D, instruction.Context.Pc, _currentBlock, branchType);
AddEdge(instruction.D == 1 ? 0 : instruction.D, instruction.Context.Pc, _currentBlock, BranchType.Can);
break;
}
case OpCode.ForNLoop:
case OpCode.JumpIf:
case OpCode.JumpIfNot:
case OpCode.JumpIfEq:
Expand All @@ -103,15 +108,7 @@ private void AddInstruction(Instruction instruction)
case OpCode.JumpIfLt:
case OpCode.JumpIfNotLt:
{
var branchType = instruction.D > 0 ? BranchType.Can : BranchType.Never;

AddEdge(instruction.D, instruction.Context.Pc, _currentBlock, branchType);
break;
}
default:
{
if (_currentBlock != oldBlock)
oldBlock.AddEdge(_currentBlock);
AddEdge(instruction.D, instruction.Context.Pc, _currentBlock, BranchType.Can);
break;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Unluau/IR/Writers/DotWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override bool Visit(ProtoType protoType)
_graph!.Add(_protoSubGraph);

// Visit all basic blocks in the control flow graph.
protoType.ControlFlow.Accept(this);
protoType.ControlFlow!.Accept(this);

return false;
}
Expand Down Expand Up @@ -144,7 +144,9 @@ public override bool Visit(Edge edge)
.From($"block_{edge.Source.GetHashCode()}").To($"block_{edge.Target.GetHashCode()}")
.WithArrowHead(DotEdgeArrowType.Vee)
.WithArrowTail(DotEdgeArrowType.None)
.WithAttribute("fontname", "Helvetica");
.WithAttribute("fontname", "Helvetica")
.WithAttribute("headport", "n")
.WithAttribute("tailport", "s");

if (_edgeName != null)
myEdge = myEdge.WithLabel(_edgeName);
Expand Down

0 comments on commit ae96652

Please sign in to comment.