Exemplo n.º 1
0
        private static void PrintCode(Lua.Proto f)
        {
            Instruction[] code = f.code;
            int           pc, n = f.sizecode;

            for (pc = 0; pc < n; pc++)
            {
                Instruction i    = f.code[pc];
                OpCode      o    = Lua.GET_OPCODE(i);
                int         a    = Lua.GETARG_A(i);
                int         b    = Lua.GETARG_B(i);
                int         c    = Lua.GETARG_C(i);
                int         ax   = Lua.GETARG_Ax(i);
                int         bx   = Lua.GETARG_Bx(i);
                int         sbx  = Lua.GETARG_sBx(i);
                int         line = Lua.getfuncline(f, pc);
                Lua.printf("\t%d\t", pc + 1);
                if (line > 0)
                {
                    Lua.printf("[%d]\t", line);
                }
                else
                {
                    Lua.printf("[-]\t");
                }
                Lua.printf("%-9s\t", Lua.luaP_opnames[(int)o]);
                switch (Lua.getOpMode(o))
                {
                case OpMode.iABC:
                    Lua.printf("%d", a);
                    if (Lua.getBMode(o) != OpArgMask.OpArgN)
                    {
                        Lua.printf(" %d", Lua.ISK(b) != 0 ? (MYK(Lua.INDEXK(b))) : b);
                    }
                    if (Lua.getCMode(o) != OpArgMask.OpArgN)
                    {
                        Lua.printf(" %d", Lua.ISK(c) != 0 ? (MYK(Lua.INDEXK(c))) : c);
                    }
                    break;

                case OpMode.iABx:
                    Lua.printf("%d", a);
                    if (Lua.getBMode(o) == OpArgMask.OpArgK)
                    {
                        Lua.printf(" %d", MYK(bx));
                    }
                    if (Lua.getBMode(o) == OpArgMask.OpArgU)
                    {
                        Lua.printf(" %d", bx);
                    }
                    break;

                case OpMode.iAsBx:
                    Lua.printf("%d %d", a, sbx);
                    break;

                case OpMode.iAx:
                    Lua.printf("%d", MYK(ax));
                    break;
                }
                switch (o)
                {
                case OpCode.OP_LOADK:
                    Lua.printf("\t; "); PrintConstant(f, bx);
                    break;

                case OpCode.OP_GETUPVAL:
                case OpCode.OP_SETUPVAL:
                    Lua.printf("\t; %s", UPVALNAME(b, f));
                    break;

                case OpCode.OP_GETTABUP:
                    Lua.printf("\t; %s", UPVALNAME(b, f));
                    if (Lua.ISK(c) != 0)
                    {
                        Lua.printf(" "); PrintConstant(f, Lua.INDEXK(c));
                    }
                    break;

                case OpCode.OP_SETTABUP:
                    Lua.printf("\t; %s", UPVALNAME(a, f));
                    if (Lua.ISK(b) != 0)
                    {
                        Lua.printf(" "); PrintConstant(f, Lua.INDEXK(b));
                    }
                    if (Lua.ISK(c) != 0)
                    {
                        Lua.printf(" "); PrintConstant(f, Lua.INDEXK(c));
                    }
                    break;

                case OpCode.OP_GETTABLE:
                case OpCode.OP_SELF:
                    if (Lua.ISK(c) != 0)
                    {
                        Lua.printf("\t; "); PrintConstant(f, Lua.INDEXK(c));
                    }
                    break;

                case OpCode.OP_SETTABLE:
                case OpCode.OP_ADD:
                case OpCode.OP_SUB:
                case OpCode.OP_MUL:
                case OpCode.OP_DIV:
                case OpCode.OP_POW:
                case OpCode.OP_EQ:
                case OpCode.OP_LT:
                case OpCode.OP_LE:
                    if (Lua.ISK(b) != 0 || Lua.ISK(c) != 0)
                    {
                        Lua.printf("\t; ");
                        if (Lua.ISK(b) != 0)
                        {
                            PrintConstant(f, Lua.INDEXK(b));
                        }
                        else
                        {
                            Lua.printf("-");
                        }
                        Lua.printf(" ");
                        if (Lua.ISK(c) != 0)
                        {
                            PrintConstant(f, Lua.INDEXK(c));
                        }
                        else
                        {
                            Lua.printf("-");
                        }
                    }
                    break;

                case OpCode.OP_JMP:
                case OpCode.OP_FORLOOP:
                case OpCode.OP_FORPREP:
                case OpCode.OP_TFORLOOP:
                    Lua.printf("\t; to %d", sbx + pc + 2);
                    break;

                case OpCode.OP_CLOSURE:
                    Lua.printf("\t; %p", VOID(f.p[bx]));
                    break;

                case OpCode.OP_SETLIST:
                    if (c == 0)
                    {
                        Lua.printf("\t; %d", (int)code[++pc]);
                    }
                    else
                    {
                        Lua.printf("\t; %d", c);
                    }
                    break;

                case OpCode.OP_EXTRAARG:
                    Lua.printf("\t; "); PrintConstant(f, ax);
                    break;

                default:
                    break;
                }
                Lua.printf("\n");
            }
        }