Skip to content

Commit

Permalink
update to support new neo (#182)
Browse files Browse the repository at this point in the history
* samllfix about

LTE->LE
GTE->GT

syscall change

* opcode small change

xswap chain->reverse

* smallchange

arraysize->size

* 修改了若干

ldarg
ltarg
ldloc
stloc
ldsfld
stsfld

* 修改了所有編譯問題

已經編譯通過

unittest 逐個檢查中

* fix initslot problem.

* 升级地址转换到32位

* fix 1 push change
fix 1 32addr conv bug

* fix ldloca and initobj logic for new neo opcodes.

* 1.fix framework
"System.ExecutionEngine.GetScriptContainer"->"System.Runtime.GetScriptContainer"
2.fix new biginteger op
pass all unittest for compiler now.

* change unittest for shift
neo-vm do not allow shift <0

* update opcode in framework.
add contact.callex in framework.

* fix all unittest except native.

* Format

* Fix some tests

* Fix native.Deploy

* Fix native

* Restore calling test for coverage

* Clean code

* Clean code

Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
lightszero and shargon committed Jan 20, 2020
1 parent 88eb342 commit 20ce296
Show file tree
Hide file tree
Showing 28 changed files with 706 additions and 956 deletions.
137 changes: 36 additions & 101 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Common.cs
Expand Up @@ -35,7 +35,6 @@ private NeoCode _Insert1(VM.OpCode code, string comment, NeoMethod to, byte[] da
private NeoCode _InsertPush(byte[] data, string comment, NeoMethod to)
{
if (data.Length == 0) return _Insert1(VM.OpCode.PUSH0, comment, to);
if (data.Length <= 75) return _Insert1((VM.OpCode)data.Length, comment, to, data);
byte prefixLen;
VM.OpCode code;
if (data.Length <= byte.MaxValue)
Expand Down Expand Up @@ -63,7 +62,7 @@ private NeoCode _InsertPush(int i, string comment, NeoMethod to)
{
if (i == 0) return _Insert1(VM.OpCode.PUSH0, comment, to);
if (i == -1) return _Insert1(VM.OpCode.PUSHM1, comment, to);
if (i > 0 && i <= 16) return _Insert1((VM.OpCode)(byte)i + 0x50, comment, to);
if (i > 0 && i <= 16) return _Insert1(VM.OpCode.PUSH0 + (byte)i, comment, to);
return _InsertPush(((BigInteger)i).ToByteArray(), comment, to);
}

Expand Down Expand Up @@ -97,7 +96,6 @@ private NeoCode _Convert1by1(VM.OpCode code, OpCode src, NeoMethod to, byte[] da
private NeoCode _ConvertPush(byte[] data, OpCode src, NeoMethod to)
{
if (data.Length == 0) return _Convert1by1(VM.OpCode.PUSH0, src, to);
if (data.Length <= 75) return _Convert1by1((VM.OpCode)data.Length, src, to, data);
byte prefixLen;
VM.OpCode code;
if (data.Length <= byte.MaxValue)
Expand Down Expand Up @@ -125,7 +123,7 @@ private NeoCode _ConvertPush(long i, OpCode src, NeoMethod to)
{
if (i == 0) return _Convert1by1(VM.OpCode.PUSH0, src, to);
if (i == -1) return _Convert1by1(VM.OpCode.PUSHM1, src, to);
if (i > 0 && i <= 16) return _Convert1by1((VM.OpCode)(byte)i + 0x50, src, to);
if (i > 0 && i <= 16) return _Convert1by1(VM.OpCode.PUSH0 + (byte)i, src, to);
return _ConvertPush(((BigInteger)i).ToByteArray(), src, to);
}
private int _ConvertPushI8WithConv(ILMethod from, long i, OpCode src, NeoMethod to)
Expand Down Expand Up @@ -228,21 +226,19 @@ private int _ConvertPushI4WithConv(ILMethod from, int i, OpCode src, NeoMethod t

private void _insertSharedStaticVarCode(NeoMethod to)
{
if (this.outModule.mapFields.Count > 255)
throw new Exception("too mush static fields");
//insert init constvalue part
_InsertPush(this.outModule.mapFields.Count, "static var", to);
_Insert1(VM.OpCode.NEWARRAY, "", to);
_Insert1(VM.OpCode.TOALTSTACK, "", to);

byte count = (byte)this.outModule.mapFields.Count;
//INITSSLOT with a u8 len
if (count > 0)
{
_Insert1(VM.OpCode.INITSSLOT, "", to, new byte[] { count });
}
foreach (var defvar in this.outModule.staticfieldsWithConstValue)
{
if (this.outModule.mapFields.TryGetValue(defvar.Key, out NeoField field))
{
//array
_Insert1(VM.OpCode.DUPFROMALTSTACKBOTTOM, "", to);

//index
_ConvertPush(field.index, null, to);

//value
#region insertValue
//this static var had a default value.
Expand Down Expand Up @@ -280,7 +276,16 @@ private void _insertSharedStaticVarCode(NeoMethod to)
_Convert1by1(VM.OpCode.PUSHNULL, null, to);
}
#endregion
_Insert1(VM.OpCode.SETITEM, "", to);

if (field.index < 7)
{
_Insert1(VM.OpCode.STSFLD0 + (byte)field.index, "", to);
}
else
{
var fieldIndex = (byte)field.index;
_Insert1(VM.OpCode.STSFLD, "", to, new byte[] { fieldIndex });
}
}
}

Expand All @@ -293,102 +298,32 @@ private void _insertSharedStaticVarCode(NeoMethod to)

private void _insertBeginCode(ILMethod from, NeoMethod to)
{
////压入深度临时栈
//_Insert1(VM.OpCode.DEPTH, "record depth.", to);
//_Insert1(VM.OpCode.TOALTSTACK, "", to);

////初始化临时槽位位置
//foreach (var src in from.body_Variables)
//{
// to.body_Variables.Add(new ILParam(src.name, src.type));
// _InsertPush(0, "body_Variables init", to);
//}

//新玩法,用一个数组,应该能减少指令数量
_InsertPush(from.paramtypes.Count + from.body_Variables.Count, "begincode", to);
_Insert1(VM.OpCode.NEWARRAY, "", to);
_Insert1(VM.OpCode.TOALTSTACK, "", to);
//移动参数槽位
for (var i = 0; i < from.paramtypes.Count; i++)
if (from.paramtypes.Count > 255)
throw new Exception("too mush params in:" + from);
if (from.body_Variables.Count > 255)
throw new Exception("too mush local varibles in:" + from);

byte paramcount = (byte)from.paramtypes.Count;
byte varcount = (byte)from.body_Variables.Count;
if (paramcount + varcount > 0)
{
//getarray
_Insert1(VM.OpCode.FROMALTSTACK, "set param:" + i, to);
_Insert1(VM.OpCode.DUP, null, to);
_Insert1(VM.OpCode.TOALTSTACK, null, to);

_InsertPush(i, "", to); //Array pos

_InsertPush(2, "", to); //Array item
_Insert1(VM.OpCode.ROLL, null, to);

_Insert1(VM.OpCode.SETITEM, null, to);
_Insert1(VM.OpCode.INITSLOT, "begincode", to, new byte[] { varcount, paramcount });
}
}
private void _insertBeginCodeEntry(NeoMethod to)
{
_InsertPush(2, "begincode", to);
_Insert1(VM.OpCode.NEWARRAY, "", to);
_Insert1(VM.OpCode.TOALTSTACK, "", to);
//移动参数槽位
for (var i = 0; i < 2; i++)
{
//getarray
_Insert1(VM.OpCode.FROMALTSTACK, "set param:" + i, to);
_Insert1(VM.OpCode.DUP, null, to);
_Insert1(VM.OpCode.TOALTSTACK, null, to);

_InsertPush(i, "", to); //Array pos

_InsertPush(2, "", to); //Array item
_Insert1(VM.OpCode.ROLL, null, to);

_Insert1(VM.OpCode.SETITEM, null, to);
}
byte paramcount = (byte)2;
byte varcount = (byte)0;
_Insert1(VM.OpCode.INITSLOT, "begincode", to, new byte[] { varcount, paramcount });
}

private void _insertEndCode(NeoMethod to, OpCode src)
{
////占位不谢
_Convert1by1(VM.OpCode.NOP, src, to);

////移除临时槽位
////drop body_Variables
//for (var i = 0; i < from.body_Variables.Count; i++)
//{
// _Insert1(VM.OpCode.DEPTH, "body_Variables drop", to, null);
// _Insert1(VM.OpCode.DEC, null, to, null);

// //push olddepth
// _Insert1(VM.OpCode.FROMALTSTACK, null, to);
// _Insert1(VM.OpCode.DUP, null, to);
// _Insert1(VM.OpCode.TOALTSTACK, null, to);
// //(d-1)-olddepth
// _Insert1(VM.OpCode.SUB, null, to);

// _Insert1(VM.OpCode.XDROP, null, to, null);
//}
////移除参数槽位
//for (var i = 0; i < from.paramtypes.Count; i++)
//{
// //d
// _Insert1(VM.OpCode.DEPTH, "param drop", to, null);

// //push olddepth
// _Insert1(VM.OpCode.FROMALTSTACK, null, to);
// _Insert1(VM.OpCode.DUP, null, to);
// _Insert1(VM.OpCode.DEC, null, to);//深度-1
// _Insert1(VM.OpCode.TOALTSTACK, null, to);

// //(d)-olddepth
// _Insert1(VM.OpCode.SUB, null, to);

// _Insert1(VM.OpCode.XDROP, null, to, null);

//}
//no need to clear altstack.

//移除深度临时栈
_Insert1(VM.OpCode.FROMALTSTACK, "endcode", to);
_Insert1(VM.OpCode.DROP, "", to);
////移除深度临时栈
//_Insert1(VM.OpCode.FROMALTSTACK, "endcode", to);
//_Insert1(VM.OpCode.DROP, "", to);
}
}
}

0 comments on commit 20ce296

Please sign in to comment.