public void TallyOpcode(Neo.VM.OpCode opcode, double opCost)
        {
            SourceFileLine sfl = new SourceFileLine(_filename, _lineno);

            string key = _filename + ":" + _lineno.ToString();

            if (dictStmtInfo.Keys.Contains(key))
            {
                SourceStmtInfo ssi;
                dictStmtInfo.TryGetValue(key, out ssi);
                ssi._stmtOpcodeCount[(int)opcode]++;
                ssi._stmtOpcodeCost[(int)opcode] = ssi._stmtOpcodeCount[(int)opcode] * opcodeCosts[(int)opcode];
            }
            else
            {
                SourceStmtInfo ssi = new SourceStmtInfo();
                ssi._filelineo  = sfl;
                ssi._sourceStmt = _sourceString;
                ssi._stmtOpcodeCount[(int)opcode] = 1;
                dictStmtInfo.Add(key, ssi);
            }

            if (!opcodeUsed[(int)opcode])
            {
                opcodeCosts[(int)opcode] = opCost;
                opcodeNames[(int)opcode] = opcode.ToString();
                opcodeUsed[(int)opcode]  = true;
            }
        }
Esempio n. 2
0
        static void CompileExpression(CompileContext context, Neo.ASML.Node.ASMFunction func, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax expression, string comment = null)
        {
            if (expression is Microsoft.CodeAnalysis.CSharp.Syntax.LiteralExpressionSyntax)
            {
                var lit = expression as Microsoft.CodeAnalysis.CSharp.Syntax.LiteralExpressionSyntax;
                var v   = lit.Token.ValueText;
                func.nodes.Add(new Neo.ASML.Node.ASMInstruction()
                {
                    opcode = Neo.ASML.Node.ASMOpCode.CreatePush(), valuetext = v, commentRight = comment
                });
                return;
            }
            else if (expression is Microsoft.CodeAnalysis.CSharp.Syntax.BinaryExpressionSyntax)
            {
                var binary = expression as Microsoft.CodeAnalysis.CSharp.Syntax.BinaryExpressionSyntax;
                if (comment != null)
                {
                    func.nodes.Add(new Neo.ASML.Node.ASMComment()
                    {
                        text = comment
                    });
                }

                CompileExpression(context, func, binary.Left);
                CompileExpression(context, func, binary.Right);

                Neo.VM.OpCode opcode = Neo.VM.OpCode.NOP;
                if (binary.OperatorToken.ValueText == "+")
                {
                    opcode = Neo.VM.OpCode.ADD;
                }
                else if (binary.OperatorToken.ValueText == "-")
                {
                    opcode = Neo.VM.OpCode.DEC;
                }
                if (binary.OperatorToken.ValueText == "*")
                {
                    opcode = Neo.VM.OpCode.MUL;
                }
                if (binary.OperatorToken.ValueText == "/")
                {
                    opcode = Neo.VM.OpCode.DIV;
                }

                func.nodes.Add(new Neo.ASML.Node.ASMInstruction()
                {
                    opcode = Neo.ASML.Node.ASMOpCode.Create(opcode)
                });
            }
            else if (expression is Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax)
            {
                var id       = expression as Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax;
                var varname  = id.Identifier.ValueText;
                var varindex = context.variables.IndexOf(varname);
                if (varindex >= 0)
                {
                    //this is a LDLoc op
                    func.nodes.Add(new Neo.ASML.Node.ASMComment()
                    {
                        text = "//" + varname
                    });
                    //push setitem
                    func.nodes.Add(new Neo.ASML.Node.ASMInstruction()
                    {
                        opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.DUPFROMALTSTACK), commentRight = "//variables array"
                    });
                    func.nodes.Add(new Neo.ASML.Node.ASMInstruction()
                    {
                        opcode = Neo.ASML.Node.ASMOpCode.CreatePush(), valuetext = varindex.ToString(), commentRight = "//index"
                    });
                    func.nodes.Add(new Neo.ASML.Node.ASMInstruction()
                    {
                        opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.PICKITEM)
                    });
                }
            }
            else
            {
                var type = expression.GetType().ToString();
                throw new Exception("not support type:" + type);
            }
        }