Ejemplo n.º 1
0
void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
{
    PycBuffer source(code->code()->value(), code->code()->length());

    int opcode, operand;
    int pos = 0;
    while (!source.atEof()) {
        for (int i=0; i<indent; i++)
            fprintf(pyc_output, "    ");
        fprintf(pyc_output, "[%7X] ", pos);   // Current bytecode position

        bc_next(source, mod, opcode, operand, pos);
        fprintf(pyc_output, "%-24s", Pyc::OpcodeName(opcode));

        if (opcode >= Pyc::PYC_HAVE_ARG) {
            if (Pyc::IsConstArg(opcode)) {
                fprintf(pyc_output, "%d: ", operand);
                print_const(code->getConst(operand), mod);
            } else if (Pyc::IsNameArg(opcode)) {
                fprintf(pyc_output, "%d: %s", operand, code->getName(operand)->value());
            } else if (Pyc::IsVarNameArg(opcode)) {
                fprintf(pyc_output, "%d: %s", operand, code->getVarName(operand)->value());
            } else if (Pyc::IsCellArg(opcode)) {
                fprintf(pyc_output, "%d: ", operand);
                print_const(code->getCellVar(operand), mod);
            } else if (Pyc::IsJumpOffsetArg(opcode)) {
                fprintf(pyc_output, "%d (to %d)", operand, pos+operand);
            } else {
                fprintf(pyc_output, "%d", operand);
            }
        }
        fprintf(pyc_output, "\n");
    }
}
Ejemplo n.º 2
0
void dis(Chunk *chunk) {
    int i;
    for (i = 0; i < chunk->numinstructions; i++) {
        int instruction = chunk->instructions[i];

        OpCode o = GET_O(instruction);
        int a = GET_A(instruction);
        int b = GET_B(instruction);
        int c = GET_C(instruction);

        switch (o) {
                case OP_RETURN:
                case OP_LEAVE_TRY:
                    printf("%d\t%-15s%d", i + 1, opcode_names[o], b);
                    break;

                case OP_THROW:
                    printf("%d\t%-15s%d", i + 1, opcode_names[0], a);
                    break;

                case OP_MOVE:
                case OP_NEG:
                case OP_NOT:
                {
                    printf("%d\t%-15s%d %d", i + 1, opcode_names[o], a, b);

                    if (b > 255) {
                        printf("\t; b=");
                        print_const(chunk->constants[b - 256]);
                    }
                } break;

                case OP_GETUPVAR:
                case OP_SETUPVAR:
                case OP_CLOSURE:
                    printf("%d\t%-15s%d %d", i + 1, opcode_names[o], a, b);
                    break;

                case OP_JUMP:
                case OP_JUMP_TRUE:
                case OP_JUMP_FALSE:
                {
                    int offset = c == 1 ? -b : b;
                    printf("%d\t%-15s%d %d\t; j=%d", i + 1, opcode_names[o], a, b, i + offset + 2);
                } break;

                case OP_ADD:
                case OP_SUB:
                case OP_MUL:
                case OP_DIV:
                case OP_MOD:
                case OP_POW:
                case OP_EQ:
                case OP_LT:
                case OP_LE:
                {
                    printf("%d\t%-15s%d %d %d", i + 1, opcode_names[o], a, b, c);

                    if (a > 255) {
                        printf("\t; a=");
                        print_const(chunk->constants[a - 256]);
                    }

                    if (b > 255) {
                        printf("\t; b=");
                        print_const(chunk->constants[b - 256]);
                    }
                } break;

                case OP_CALL:
                    printf("%d\t%-15s%d %d %d", i + 1, opcode_names[o], a, b, c);
                    break;

                case OP_ENTER_TRY:
                    printf("%d\t%-15s%d\t; j=%d", i + 1, opcode_names[o], b, i + b + 1);
                    break;
        }

        printf("\n");
    }

    for (i = 0; i < chunk->numchildren; i++) {
        dis(chunk->children[i]);
    }
}
Ejemplo n.º 3
0
void print_const(PycRef<PycObject> obj, PycModule* mod)
{
    switch (obj->type()) {
    case PycObject::TYPE_STRING:
    case PycObject::TYPE_STRINGREF:
    case PycObject::TYPE_INTERNED:
        OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 'b' : 0);
        break;
    case PycObject::TYPE_UNICODE:
        OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 0 : 'u');
        break;
    case PycObject::TYPE_TUPLE:
        {
            fprintf(pyc_output, "(");
            PycTuple::value_t values = obj.cast<PycTuple>()->values();
            PycTuple::value_t::const_iterator it = values.begin();
            if (it != values.end()) {
                print_const(*it, mod);
                while (++it != values.end()) {
                    fprintf(pyc_output, ", ");
                    print_const(*it, mod);
                }
            }
            if (values.size() == 1)
                fprintf(pyc_output, ",)");
            else
                fprintf(pyc_output, ")");
        }
        break;
    case PycObject::TYPE_LIST:
        {
            fprintf(pyc_output, "[");
            PycList::value_t values = obj.cast<PycList>()->values();
            PycList::value_t::const_iterator it = values.begin();
            if (it != values.end()) {
                print_const(*it, mod);
                while (++it != values.end()) {
                    fprintf(pyc_output, ", ");
                    print_const(*it, mod);
                }
            }
            fprintf(pyc_output, "]");
        }
        break;
    case PycObject::TYPE_DICT:
        {
            fprintf(pyc_output, "{");
            PycDict::key_t keys = obj.cast<PycDict>()->keys();
            PycDict::value_t values = obj.cast<PycDict>()->values();
            PycDict::key_t::const_iterator ki = keys.begin();
            PycDict::value_t::const_iterator vi = values.begin();
            if (ki != keys.end()) {
                print_const(*ki, mod);
                fprintf(pyc_output, ": ");
                print_const(*vi, mod);
                while (++ki != keys.end()) {
                    ++vi;
                    fprintf(pyc_output, ", ");
                    print_const(*ki, mod);
                    fprintf(pyc_output, ": ");
                    print_const(*vi, mod);
                }
            }
            fprintf(pyc_output, "}");
        }
        break;
    case PycObject::TYPE_SET:
        {
            fprintf(pyc_output, "{");
            PycSet::value_t values = obj.cast<PycSet>()->values();
            PycSet::value_t::const_iterator it = values.begin();
            if (it != values.end()) {
                print_const(*it, mod);
                while (++it != values.end()) {
                    fprintf(pyc_output, ", ");
                    print_const(*it, mod);
                }
            }
            fprintf(pyc_output, "}");
        }
        break;
    case PycObject::TYPE_NONE:
        fprintf(pyc_output, "None");
        break;
    case PycObject::TYPE_TRUE:
        fprintf(pyc_output, "True");
        break;
    case PycObject::TYPE_FALSE:
        fprintf(pyc_output, "False");
        break;
    case PycObject::TYPE_INT:
        fprintf(pyc_output, "%d", obj.cast<PycInt>()->value());
        break;
    case PycObject::TYPE_LONG:
        fprintf(pyc_output, "%s", obj.cast<PycLong>()->repr().c_str());
        break;
    case PycObject::TYPE_FLOAT:
        fprintf(pyc_output, "%s", obj.cast<PycFloat>()->value());
        break;
    case PycObject::TYPE_COMPLEX:
        fprintf(pyc_output, "(%s+%sj)", obj.cast<PycComplex>()->value(),
                                        obj.cast<PycComplex>()->imag());
        break;
    case PycObject::TYPE_BINARY_FLOAT:
        fprintf(pyc_output, "%g", obj.cast<PycCFloat>()->value());
        break;
    case PycObject::TYPE_BINARY_COMPLEX:
        fprintf(pyc_output, "(%g+%gj)", obj.cast<PycCComplex>()->value(),
                                        obj.cast<PycCComplex>()->imag());
        break;
    case PycObject::TYPE_CODE:
    case PycObject::TYPE_CODE2:
        fprintf(pyc_output, "<CODE> %s", obj.cast<PycCode>()->name()->value());
        break;
    }
}
Ejemplo n.º 4
0
int main (int argc, char const* argv[])
{
    print_const("_POSIX_ARG_MAX", _POSIX_ARG_MAX);
    print_const("_POSIX_CHILD_MAX", _POSIX_CHILD_MAX);
    print_const("_POSIX_LINK_MAX", _POSIX_LINK_MAX);
    print_const("_POSIX_MAX_CANON", _POSIX_MAX_CANON);
    print_const("_POSIX_MAX_INPUT", _POSIX_MAX_INPUT);
    print_const("_POSIX_NAME_MAX", _POSIX_NAME_MAX);
    print_const("_POSIX_NGROUPS_MAX", _POSIX_NGROUPS_MAX);
    print_const("_POSIX_OPEN_MAX", _POSIX_OPEN_MAX);
    print_const("_POSIX_PATH_MAX", _POSIX_PATH_MAX);
    print_const("_POSIX_PIPE_BUF", _POSIX_PIPE_BUF);
    print_const("_POSIX_SSIZE_MAX", _POSIX_SSIZE_MAX);
    print_const("_POSIX_STREAM_MAX", _POSIX_STREAM_MAX);
    print_const("_POSIX_TZNAME_MAX", _POSIX_TZNAME_MAX);

    return 0;
}