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

update to support new neo #182

Merged
merged 23 commits into from Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}
}