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

Add abi offset #219

Merged
merged 48 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
d353ee9
add abi offset
Mar 24, 2020
c90cb3e
fix
Mar 24, 2020
fcb7dc2
clean optimizer
lightszero Mar 24, 2020
4af85aa
Clean code
shargon Mar 24, 2020
132a190
Update NefOptimizeTool.cs
shargon Mar 24, 2020
5e7e947
fix GetAddrConvertTable bug,return all offsets now
lightszero Mar 25, 2020
432605c
format
Mar 25, 2020
1fd03cd
format
Mar 25, 2020
1de60d1
Merge branch 'master' into add-abi-offset
erikzhang Mar 25, 2020
e8aee62
Clean code
shargon Mar 25, 2020
0fd7964
add abi-offset-unitTest
Mar 25, 2020
e27f299
Merge branch 'add-abi-offset' of https://github.com/ShawnYun/neo-devp…
Mar 25, 2020
739f653
Rename Contract_AbiOffset.cs to Contract_ABIOffset.cs
ShawnYun Mar 25, 2020
1c74e62
fix
Mar 26, 2020
d469495
Clean code
shargon Mar 26, 2020
a524038
Optimize
shargon Mar 26, 2020
4262ba4
Fix
shargon Mar 26, 2020
b162242
fix NDEBUG
shargon Mar 26, 2020
1b0ce50
Fix NDEBUG
shargon Mar 26, 2020
1331c73
Merge branch 'master' into add-abi-offset
lightszero Mar 29, 2020
f770a97
Merge branch 'master' into add-abi-offset
shargon Mar 30, 2020
57b94ac
modify abi offset uintTest
Mar 31, 2020
71869a9
Merge branch 'master' into add-abi-offset
shargon Apr 7, 2020
24fec80
Merge branch 'master' into add-abi-offset
lightszero Apr 13, 2020
d933683
Merge branch 'master' of https://github.com/neo-project/neo-devpack-d…
Apr 16, 2020
c9453a6
Merge branch 'master' into add-abi-offset
lightszero Apr 16, 2020
93f292a
add initializing static variables
Apr 20, 2020
4b46326
Merge branch 'add-abi-offset' of https://github.com/ShawnYun/neo-devp…
Apr 20, 2020
9af0429
format
Apr 20, 2020
1faaebd
format
Apr 20, 2020
8d767bb
modify UTs
Apr 21, 2020
bcaa2e4
fix and rename
Apr 21, 2020
a7dcd5f
modify ContractTest
Apr 23, 2020
2d23cbc
fix conflicts
Apr 23, 2020
e7b114f
format
Apr 23, 2020
9c6243e
fix
Apr 24, 2020
bfde89a
fix TestEngine and modify _initialize
Apr 24, 2020
ab99dc3
fix
Apr 24, 2020
3165a34
fix conflicts
Apr 24, 2020
3b5b466
format
Apr 24, 2020
b3cd688
fix void return
Apr 24, 2020
a424b24
Update Opcodes
shargon Apr 24, 2020
806f988
resolve review issues
Apr 24, 2020
f70aa8d
Merge branch 'add-abi-offset' of https://github.com/ShawnYun/neo-devp…
Apr 24, 2020
9140bbb
fix
Apr 24, 2020
54ad1b0
fix
Apr 24, 2020
5c01853
fix
Apr 24, 2020
575c3d7
resolve conflicts
Apr 26, 2020
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
21 changes: 4 additions & 17 deletions src/Neo.Compiler.MSIL/FuncExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,30 @@ public static MyJson.JsonNode_Object Export(NeoModule module, byte[] script)
}
outjson.SetDictValue("hash", sb.ToString());

//entrypoint
var entryPoint = "main";
var mainmethod = module.mapMethods[module.mainMethod];
if (mainmethod != null)
{
entryPoint = mainmethod.displayName;
}

//functions
var methods = new MyJson.JsonNode_Array();
outjson["methods"] = methods;

List<string> names = new List<string>();
foreach (var function in module.mapMethods)
{
var offset = function.Value.funcaddr;
if (offset == -1) continue;
var mm = function.Value;
if (mm.inSmartContract == false)
continue;
if (mm.isPublic == false)
continue;

var funcsign = new MyJson.JsonNode_Object();
if (function.Value.displayName == entryPoint)
{
// This is the entryPoint
outjson.SetDictValue("entryPoint", funcsign);
}
else
{
methods.Add(funcsign);
}
methods.Add(funcsign);
funcsign.SetDictValue("name", function.Value.displayName);
if (names.Contains(function.Value.displayName))
{
throw new Exception("abi not allow same name functions");
}
names.Add(function.Value.displayName);
funcsign.SetDictValue("offset", offset.ToString());
MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
funcsign["parameters"] = funcparams;
if (mm.paramtypes != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Compiler.MSIL/MSIL/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public NeoModule Convert(ILModule _in, ConvOption option = null)
if (l.Value == m)
{
if (mainmethod != "")
throw new Exception("Have too mush EntryPoint,Check it.");
throw new Exception("Have too much EntryPoint,Check it.");
mainmethod = key;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Neo.Compiler.MSIL/Optimizer/NefInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class NefInstruction : INefItem

public OpCode OpCode { get; private set; }
public uint Size => (1 + DataPrefixSize + DataSize);
public bool IsMethodEntry = false;
public string MethodName = null;

private uint DataPrefixSize => GetOperandPrefixSize(OpCode);
private uint DataSize => DataPrefixSize > 0 ? (uint)(Data?.Length ?? 0) : GetOperandSize(OpCode);
Expand Down
27 changes: 22 additions & 5 deletions src/Neo.Compiler.MSIL/Optimizer/NefOptimizeTool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace Neo.Compiler.Optimizer
Expand All @@ -10,9 +11,9 @@ public static class NefOptimizeTool
/// </summary>
/// <param name="script">Script</param>
/// <returns>Optimized script</returns>
public static byte[] Optimize(byte[] script)
public static byte[] Optimize(byte[] script, NeoModule module = null)
{
return Optimize(script, new OptimizeParserType[] { OptimizeParserType.DELETE_DEAD_CODDE, OptimizeParserType.USE_SHORT_ADDRESS });
return Optimize(script, new OptimizeParserType[] { OptimizeParserType.DELETE_DEAD_CODDE, OptimizeParserType.USE_SHORT_ADDRESS }, module);
}

/// <summary>
Expand All @@ -25,7 +26,7 @@ public static byte[] Optimize(byte[] script)
/// <para> DELETE_NOP -- delete nop parser</para>
/// <para> DELETE_USELESS_JMP -- delete useless jmp parser, eg: JPM 2</para></param>
/// <returns>Optimized script</returns>
public static byte[] Optimize(byte[] script, OptimizeParserType[] parserTypes)
public static byte[] Optimize(byte[] script, OptimizeParserType[] parserTypes, NeoModule module = null)
ShawnYun marked this conversation as resolved.
Show resolved Hide resolved
{
var optimizer = new NefOptimizer();

Expand All @@ -41,18 +42,34 @@ public static byte[] Optimize(byte[] script, OptimizeParserType[] parserTypes)
optimizer.AddOptimizeParser(parser);
}

//find method entry
Dictionary<string, int> methodEntry = new Dictionary<string, int>();
if (module != null)
{
foreach (var function in module.mapMethods)
{
var mm = function.Value;
if (mm.inSmartContract == false || mm.isPublic == false)
continue;
if (methodEntry.ContainsKey(function.Value.displayName))
throw new Exception("not allow same name functions");

methodEntry.Add(function.Value.displayName, function.Value.funcaddr);
}
}

//step01 Load
using (var ms = new MemoryStream(script))
{
optimizer.LoadNef(ms);
optimizer.LoadNef(ms, methodEntry);
}
//step02 doOptimize
optimizer.Optimize();

//step03 link
using (var ms = new MemoryStream())
{
optimizer.LinkNef(ms);
optimizer.LinkNef(ms, module);
var bytes = ms.ToArray();
return bytes;
}
Expand Down
38 changes: 34 additions & 4 deletions src/Neo.Compiler.MSIL/Optimizer/NefOptimizer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Compiler.Optimizer
{
Expand All @@ -16,13 +18,13 @@ public class NefOptimizer
/// Constructor
/// </summary>
/// <param name="script">Script</param>
public NefOptimizer(byte[] script = null)
public NefOptimizer(byte[] script = null, Dictionary<string, int> methodEntry = null)
{
if (script != null)
{
using (var ms = new MemoryStream(script))
{
LoadNef(ms);
LoadNef(ms, methodEntry);
}
}
}
Expand Down Expand Up @@ -57,7 +59,7 @@ public void Optimize()
/// Step01 Load
/// </summary>
/// <param name="stream">Stream</param>
public void LoadNef(Stream stream)
public void LoadNef(Stream stream, Dictionary<string, int> methodEntry = null)
{
//read all Instruction to listInst
var listInst = new List<NefInstruction>();
Expand Down Expand Up @@ -99,6 +101,13 @@ public void LoadNef(Stream stream)
Items.Add(mapLabel[curOffset]);
}
Items.Add(instruction);

if (methodEntry.ContainsValue(instruction.Offset))
{
var methodName = methodEntry.FirstOrDefault(q => q.Value == instruction.Offset).Key;
instruction.IsMethodEntry = true;
instruction.MethodName = methodName;
}
}
}

Expand Down Expand Up @@ -140,17 +149,38 @@ void RefillAddr()
}
}

public void LinkNef(Stream stream)
public void LinkNef(Stream stream, NeoModule module)
{
//Recalc Address
//collection Labels and Resort Offset
RefillAddr();

//and Link
//and find method entry offset
Dictionary<string, int> methodEntry = new Dictionary<string, int>();
foreach (var inst in this.Items)
{
if (inst is NefInstruction i)
{
i.WriteTo(stream);
if (i.IsMethodEntry == true)
methodEntry.TryAdd(i.MethodName, i.Offset);
}
}

if (module != null)
{
foreach (var function in module.mapMethods)
{
if (methodEntry.ContainsKey(function.Value.displayName))
{
function.Value.funcaddr = methodEntry[function.Value.displayName];
}
else
{
function.Value.funcaddr = -1;
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Compiler.MSIL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static int Compile(Options options)

if (options.Optimize)
{
var optimize = NefOptimizeTool.Optimize(bytes);
var optimize = NefOptimizeTool.Optimize(bytes, module);
log.Log("optimization succ " + (((bytes.Length / (optimize.Length + 0.0)) * 100.0) - 100).ToString("0.00 '%'"));
bytes = optimize;
}
Expand Down