Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optimizer error #334

Merged
merged 5 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions src/Neo.Compiler.MSIL/NeoModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,6 @@ public string GenJson()
json.ConvertToStringWithFormat(sb, 4);
return sb.ToString();
}

internal void ConvertFuncAddr()
{
foreach (var method in this.mapMethods.Values)
{
foreach (var code in method.body_Codes.Values)
{
if (code.code != VM.OpCode.NOP)
{
method.funcaddr = code.addr;
break;
}
}
}
}
}

public class NeoMethod
Expand Down
14 changes: 6 additions & 8 deletions src/Neo.Compiler.MSIL/Optimizer/NefOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ public void Optimize()
/// <param name="entryPoints">Entry points</param>
public void LoadNef(Stream stream, int[] entryPoints)
{
//read all Instruction to listInst
// Read all Instruction to listInst.
var listInst = new List<NefInstruction>();
//read all Address to listAddr
// Read all Address to listAddr.
var mapLabel = new Dictionary<int, NefLabel>();
int labelindex = 1;

//insert EntryPointLabel
// Insert EntryPoint Label.
for (var i = 0; i < entryPoints.Length; i++)
{
if (i > 0 && entryPoints[i - 1] == entryPoints[i])
{
//不允许出现一样的EntryPoint
throw new Exception("now allow same EntryPoint.");
// Same EntryPoints are not allowed
throw new Exception("Same EntryPoints are not allowed.");
}
if (!mapLabel.ContainsKey(entryPoints[i]))
{
Expand Down Expand Up @@ -116,16 +116,14 @@ public void LoadNef(Stream stream, int[] entryPoints)
labelindex++;
var label = new NefLabel(labelname, addr);
mapLabel.Add(addr, label);

}

inst.Labels[i] = mapLabel[addr].Name;
}
}
} while (inst != null);


//Add Labels and split to Methods
// Add Labels and split to Methods
Methods = new List<NefMethod>();

var curMethod = new NefMethod();
Expand Down
1 change: 0 additions & 1 deletion src/Neo.Compiler.MSIL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public static int Compile(Options options)
Dictionary<int, int> addrConvTable = null;
if (options.Optimize)
{
module.ConvertFuncAddr();
HashSet<int> entryPoints = new HashSet<int>();
foreach (var func in module.mapMethods)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework;

namespace Neo.Compiler.MSIL.TestClasses
{
public class Contract_OptimizationTest : SmartContract.Framework.SmartContract
{
private static byte[] Owner = "Ne9ipxm2sPUaetvh3ZvjhyCzRZqP355dTZ".ToScriptHash();
public static bool Verify()
ShawnYun marked this conversation as resolved.
Show resolved Hide resolved
{
return Runtime.CheckWitness(Owner);
}
}
}
11 changes: 11 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.Compiler.Optimizer;
using Neo.VM;
using System;
Expand All @@ -10,6 +11,16 @@ namespace Neo.Compiler.MSIL
[TestClass]
public class UnitTest_NefOptimizer
{
[TestMethod]
public void Test_OptimizerNopEntryPoint()
{
var testengine = new TestEngine();
var build = testengine.Build("./TestClasses/Contract_OptimizationTest.cs", false, true);

Assert.AreEqual(build.finalABI["methods"].AsList()[0].asDict()["name"].AsString(), "verify");
Assert.AreEqual(build.finalABI["methods"].AsList()[0].asDict()["offset"].AsString(), "0");
}

[TestMethod]
public void Test_Optimize_RemoveNOPS()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Build(Stream fs, Stream fspdb, bool optimizer)
List<int> entryPoints = new List<int>();
foreach (var f in converterIL.outModule.mapMethods.Values)
{
if (entryPoints.Contains(f.funcaddr) == false)
if (!entryPoints.Contains(f.funcaddr))
entryPoints.Add(f.funcaddr);
}
var opbytes = NefOptimizeTool.Optimize(finalNEF, entryPoints.ToArray(), out addrConvTable);
Expand Down