Antlr4.StringTemplate.Compiler.BytecodeDisassembler.DisassembleInstruction C# (CSharp) Метод

DisassembleInstruction() публичный Метод

public DisassembleInstruction ( System.Text.StringBuilder buf, int ip ) : int
buf System.Text.StringBuilder
ip int
Результат int
        public virtual int DisassembleInstruction(StringBuilder buf, int ip)
        {
            int opcode = code.instrs[ip];
            if (ip >= code.codeSize)
            {
                throw new ArgumentException("ip out of range: " + ip);
            }
            Instruction I = Instruction.instructions[opcode];
            if (I == null)
            {
                throw new ArgumentException("no such instruction " + opcode + " at address " + ip);
            }
            string instrName = I.name;
            buf.Append(string.Format("{0:0000}:\t{1,-14}", ip, instrName));
            ip++;
            if (I.nopnds == 0)
            {
                buf.Append("  ");
                return ip;
            }

            List<string> operands = new List<string>();
            for (int i = 0; i < I.nopnds; i++)
            {
                int opnd = GetShort(code.instrs, ip);
                ip += Instruction.OperandSizeInBytes;
                switch (I.type[i])
                {
                case OperandType.String:
                    operands.Add(ShowConstantPoolOperand(opnd));
                    break;

                case OperandType.Address:
                case OperandType.Int:
                    operands.Add(opnd.ToString());
                    break;

                default:
                    operands.Add(opnd.ToString());
                    break;
                }
            }

            for (int i = 0; i < operands.Count; i++)
            {
                string s = operands[i];
                if (i > 0)
                    buf.Append(", ");

                buf.Append(s);
            }
            return ip;
        }

Usage Example

Пример #1
0
        protected virtual void Trace(TemplateFrame frame, int ip)
        {
            Template self = frame.Template;
            StringBuilder tr = new StringBuilder();
            BytecodeDisassembler dis = new BytecodeDisassembler(self.impl);
            StringBuilder buf = new StringBuilder();
            dis.DisassembleInstruction(buf, ip);
            string name = self.impl.Name + ":";
            if (object.ReferenceEquals(self.impl.Name, Template.UnknownName))
                name = string.Empty;

            tr.Append(string.Format("{0,-40}", name + buf));
            tr.Append("\tstack=[");
            for (int i = 0; i <= sp; i++)
            {
                object o = operands[i];
                PrintForTrace(tr, frame, o);
            }

            tr.Append(" ], calls=");
            tr.Append(frame.GetEnclosingInstanceStackString());
            tr.Append(", sp=" + sp + ", nw=" + nwline);
            string s = tr.ToString();

            if (_debug)
                executeTrace.Add(s);

            if (trace)
                Console.WriteLine(s);
        }