Skip to content

Commit

Permalink
refactor: Spanify all AVM2 instruction specific IO logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulusParssinen committed Feb 3, 2024
1 parent 0db1131 commit 6e4667e
Show file tree
Hide file tree
Showing 142 changed files with 1,268 additions and 1,234 deletions.
135 changes: 67 additions & 68 deletions Flazzy/ABC/AVM2/ASCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,95 +577,94 @@ private void LoadInstructions()
var marks = new Dictionary<long, ASInstruction>();
var sharedExits = new Dictionary<long, List<Jumper>>();
var switchCases = new Dictionary<long, List<(LookUpSwitchIns, int)>>();
using (var input = new FlashReader(_body.Code))

var input = new SpanFlashReader(_body.Code);
while (input.IsDataAvailable)
{
while (input.IsDataAvailable)
{
long previousPosition = input.Position;
var instruction = ASInstruction.Create(_abc, input);
marks[previousPosition] = instruction;
long previousPosition = input.Position;
var instruction = ASInstruction.Create(_abc, ref input);
marks[previousPosition] = instruction;

_indices.Add(instruction, _indices.Count);
_instructions.Add(instruction);
_indices.Add(instruction, _indices.Count);
_instructions.Add(instruction);

if (!_opGroups.TryGetValue(instruction.OP, out List<ASInstruction> instructions))
{
instructions = new List<ASInstruction>();
_opGroups.Add(instruction.OP, instructions);
}
instructions.Add(instruction);
if (!_opGroups.TryGetValue(instruction.OP, out List<ASInstruction> instructions))
{
instructions = new List<ASInstruction>();
_opGroups.Add(instruction.OP, instructions);
}
instructions.Add(instruction);

if (sharedExits.TryGetValue(previousPosition, out List<Jumper> jumpers))
if (sharedExits.TryGetValue(previousPosition, out List<Jumper> jumpers))
{
// This is an exit position two or more jump instructions.
foreach (Jumper jumper in jumpers)
{
// This is an exit position two or more jump instructions.
foreach (Jumper jumper in jumpers)
{
JumpExits.Add(jumper, instruction);
}
sharedExits.Remove(previousPosition);
JumpExits.Add(jumper, instruction);
}
sharedExits.Remove(previousPosition);
}

if (switchCases.TryGetValue(previousPosition, out List<(LookUpSwitchIns, int)> caseExits))
if (switchCases.TryGetValue(previousPosition, out List<(LookUpSwitchIns, int)> caseExits))
{
foreach ((LookUpSwitchIns owner, int index) in caseExits)
{
foreach ((LookUpSwitchIns owner, int index) in caseExits)
{
SwitchExits[owner][index] = instruction;
}
switchCases.Remove(previousPosition);
SwitchExits[owner][index] = instruction;
}
switchCases.Remove(previousPosition);
}

if (instruction.OP == OPCode.LookUpSwitch)
{
var lookUpSwitchIns = (LookUpSwitchIns)instruction;
var offsets = new List<uint>(lookUpSwitchIns.CaseOffsets) { lookUpSwitchIns.DefaultOffset };
if (instruction.OP == OPCode.LookUpSwitch)
{
var lookUpSwitchIns = (LookUpSwitchIns)instruction;
var offsets = new List<uint>(lookUpSwitchIns.CaseOffsets) { lookUpSwitchIns.DefaultOffset };

var exits = new ASInstruction[offsets.Count];
for (int i = 0; i < offsets.Count; i++)
var exits = new ASInstruction[offsets.Count];
for (int i = 0; i < offsets.Count; i++)
{
long exitPosition = previousPosition + offsets[i];
if (exitPosition <= input.Length)
{
long exitPosition = previousPosition + offsets[i];
if (exitPosition <= input.Length)
if (!switchCases.TryGetValue(exitPosition, out caseExits))
{
if (!switchCases.TryGetValue(exitPosition, out caseExits))
{
caseExits = new List<(LookUpSwitchIns, int)>();
switchCases.Add(exitPosition, caseExits);
}
caseExits.Add((lookUpSwitchIns, i));
caseExits = new List<(LookUpSwitchIns, int)>();
switchCases.Add(exitPosition, caseExits);
}
else exits[i] = marks[exitPosition - uint.MaxValue - 1];
caseExits.Add((lookUpSwitchIns, i));
}
SwitchExits.Add(lookUpSwitchIns, exits);
else exits[i] = marks[exitPosition - uint.MaxValue - 1];
}
else if (Jumper.IsValid(instruction.OP))
{
var jumper = (Jumper)instruction;
if (jumper.Offset == 0) continue;
SwitchExits.Add(lookUpSwitchIns, exits);
}
else if (Jumper.IsValid(instruction.OP))
{
var jumper = (Jumper)instruction;
if (jumper.Offset == 0) continue;

long exitPosition = (input.Position + jumper.Offset);
if (exitPosition == input.Length)
{
// Jump exit does not exist at this (non-existent)index, do not look for exit.
continue;
}
else if (exitPosition < input.Length) // Forward jump.
long exitPosition = (input.Position + jumper.Offset);
if (exitPosition == input.Length)
{
// Jump exit does not exist at this (non-existent)index, do not look for exit.
continue;
}
else if (exitPosition < input.Length) // Forward jump.
{
if (!sharedExits.TryGetValue(exitPosition, out jumpers))
{
if (!sharedExits.TryGetValue(exitPosition, out jumpers))
{
jumpers = new List<Jumper>();
sharedExits.Add(exitPosition, jumpers);
}
jumpers.Add(jumper);
jumpers = new List<Jumper>();
sharedExits.Add(exitPosition, jumpers);
}
else // Backwards jump.
jumpers.Add(jumper);
}
else // Backwards jump.
{
long markIndex = exitPosition - uint.MaxValue - 1;
if (marks[markIndex].OP == OPCode.Label)
{
long markIndex = exitPosition - uint.MaxValue - 1;
if (marks[markIndex].OP == OPCode.Label)
{
var label = (LabelIns)marks[markIndex];
JumpExits.Add(jumper, label);
}
// TODO: Check if not adding an impossible label is fine...
var label = (LabelIns)marks[markIndex];
JumpExits.Add(jumper, label);
}
// TODO: Check if not adding an impossible label is fine...
}
}
}
Expand Down
Loading

0 comments on commit 6e4667e

Please sign in to comment.