Skip to content

Commit

Permalink
modify debuginfo in ReplaceJumpWithRet with oldSequencePointAddressToNew
Browse files Browse the repository at this point in the history
  • Loading branch information
Hecate2 committed Sep 5, 2024
1 parent be2def2 commit 84b11b3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static (NefFile, ContractManifest, JObject?) BuildOptimizedAssets(
System.Collections.Specialized.OrderedDictionary simplifiedInstructionsToAddress,
Dictionary<Instruction, Instruction> jumpSourceToTargets,
Dictionary<Instruction, (Instruction, Instruction)> trySourceToTargets,
Dictionary<int, Instruction> oldAddressToInstruction)
Dictionary<int, Instruction> oldAddressToInstruction,
Dictionary<int, int>? oldSequencePointAddressToNew = null)
{
nef.Script = OptimizedScriptBuilder.BuildScriptWithJumpTargets(
simplifiedInstructionsToAddress,
Expand All @@ -36,7 +37,9 @@ public static (NefFile, ContractManifest, JObject?) BuildOptimizedAssets(
nef.CheckSum = NefFile.ComputeChecksum(nef);
foreach (ContractMethodDescriptor method in manifest.Abi.Methods)
method.Offset = (int)simplifiedInstructionsToAddress[oldAddressToInstruction[method.Offset]]!;
debugInfo = DebugInfoBuilder.ModifyDebugInfo(debugInfo, simplifiedInstructionsToAddress, oldAddressToInstruction);
debugInfo = DebugInfoBuilder.ModifyDebugInfo(
debugInfo, simplifiedInstructionsToAddress, oldAddressToInstruction,
oldSequencePointAddressToNew: oldSequencePointAddressToNew);
return (nef, manifest, debugInfo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ static class DebugInfoBuilder
/// <returns></returns>
public static JObject? ModifyDebugInfo(JObject? debugInfo,
System.Collections.Specialized.OrderedDictionary simplifiedInstructionsToAddress,
Dictionary<int, Instruction> oldAddressToInstruction)
Dictionary<int, Instruction> oldAddressToInstruction,
Dictionary<int, int>? oldSequencePointAddressToNew = null)
{
if (debugInfo == null) return null;
//Dictionary<int, (int docId, int startLine, int startCol, int endLine, int endCol)> newAddrToSequencePoint = new();
Expand Down Expand Up @@ -58,6 +59,9 @@ static class DebugInfoBuilder
newSequencePoints.Add(new JString($"{startInstructionAddress}{sequencePointGroups[2]}"));
previousSequencePoint = startInstructionAddress;
}
else if (oldSequencePointAddressToNew != null
&& oldSequencePointAddressToNew.TryGetValue(startInstructionAddress, out int newStartInstructionAddress))
newSequencePoints.Add(new JString($"{newStartInstructionAddress}{sequencePointGroups[2]}"));
else
newSequencePoints.Add(new JString($"{previousSequencePoint}{sequencePointGroups[2]}"));
}
Expand Down
11 changes: 7 additions & 4 deletions src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Neo.Optimizer
{
static class Reachability
{
[Strategy(Priority = int.MaxValue - 4)]
[Strategy(Priority = int.MaxValue - 16)]
public static (NefFile, ContractManifest, JObject?) RemoveUncoveredInstructions(NefFile nef, ContractManifest manifest, JObject? debugInfo = null)
{
InstructionCoverage oldContractCoverage = new InstructionCoverage(nef, manifest);
Expand Down Expand Up @@ -63,7 +63,7 @@ public static Dictionary<int, BranchType>
/// <param name="manifest"></param>
/// <param name="debugInfo"></param>
/// <returns></returns>
[Strategy(Priority = int.MaxValue - 16)]
[Strategy(Priority = int.MaxValue)]
public static (NefFile, ContractManifest, JObject?) RemoveUnnecessaryJumps(NefFile nef, ContractManifest manifest, JObject? debugInfo = null)
{
Script script = nef.Script;
Expand Down Expand Up @@ -126,7 +126,7 @@ public static (NefFile, ContractManifest, JObject?) RemoveUnnecessaryJumps(NefFi
/// <param name="manifest"></param>
/// <param name="debugInfo"></param>
/// <returns></returns>
[Strategy(Priority = int.MaxValue)]
[Strategy(Priority = int.MaxValue - 4)]
public static (NefFile, ContractManifest, JObject?) ReplaceJumpWithRet(NefFile nef, ContractManifest manifest, JObject? debugInfo = null)
{
Script script = nef.Script;
Expand All @@ -138,6 +138,7 @@ public static (NefFile, ContractManifest, JObject?) ReplaceJumpWithRet(NefFile n
Dictionary<Instruction, (Instruction, Instruction)> trySourceToTargets,
Dictionary<Instruction, HashSet<Instruction>> jumpTargetToSources) =
FindAllJumpAndTrySourceToTargets(oldAddressAndInstructionsList);
Dictionary<int, int> oldSequencePointAddressToNew = new();

System.Collections.Specialized.OrderedDictionary simplifiedInstructionsToAddress = new();
int currentAddress = 0;
Expand All @@ -150,6 +151,7 @@ public static (NefFile, ContractManifest, JObject?) ReplaceJumpWithRet(NefFile n
throw new BadScriptException($"Bad {nameof(oldAddressToInstruction)}. No target found for {i} jumping from {a} to {target}");
if (dstRet.OpCode == OpCode.RET)
{
oldSequencePointAddressToNew[a] = currentAddress;
// handle the reference of the deleted JMP
jumpSourceToTargets.Remove(i);
jumpTargetToSources[dstRet].Remove(i);
Expand Down Expand Up @@ -187,7 +189,8 @@ public static (NefFile, ContractManifest, JObject?) ReplaceJumpWithRet(NefFile n
return AssetBuilder.BuildOptimizedAssets(nef, manifest, debugInfo,
simplifiedInstructionsToAddress,
jumpSourceToTargets, trySourceToTargets,
oldAddressToInstruction);
oldAddressToInstruction,
oldSequencePointAddressToNew: oldSequencePointAddressToNew);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Test_SequencePointInserter()
{
var instruction = script.GetInstruction(ip);

if (instruction.OpCode != OpCode.INITSLOT && instruction.OpCode != OpCode.RET)
if (ip != 0) // Avoid INITSLOT
{
Assert.IsTrue(points.Contains(ip), $"Offset {ip} with '{instruction.OpCode}' is not in sequence points.");
}
Expand Down

0 comments on commit 84b11b3

Please sign in to comment.