Exemple #1
0
    def interpret(self):
        self.declare_functions()

        code = self.bytecode.code
        bytecode_length = len(self.bytecode.code)

        while True:
            if self.position >= bytecode_length:
                break

            self.context.trace.line = self.bytecode.opcode_lines[self.position]

            opcode_index = ord(code[self.position])
            self.position += 1

            arg = 0
            if opcode_index > OPCODE_INDEX_DIVIDER:
                arg = ord(code[self.position]) + (
                    ord(code[self.position + 1]) << 8)
                self.position += 2

            if we_are_translated():
                for index, name in unrolling_bc:
                    if index == opcode_index:
                        getattr(self, name)(arg)
                        break
                else:
                    raise InternalError("Unknown opcode")
            else:
                opcode_name = get_opcode_name(opcode_index)
                getattr(self, opcode_name)(arg)

            # this is a return condition
            if self.position == self.RETURN_FLAG:
                break
Exemple #2
0
def disassemble(bytecode):
    """Function to disassemble code to human-readable form"""

    assert isinstance(bytecode, Bytecode)
    result = 'Bytecode object: <' \
        + str(compute_unique_id(bytecode)) \
        + '> ' + bytecode.filename + '\n'

    if bytecode.consts:
        result += '\n  Consts:\n'
        for const in enumerate(bytecode.consts):
            result += '      %4d: %s\n' % const

    if bytecode.names:
        result += '\n  Names:\n'
        for name in enumerate(bytecode.names):
            result += '      %4d: %s\n' % name

    if bytecode.declared_functions or bytecode.functions:
        result += '\n  Functions:\n'
        for number, function in enumerate(bytecode.declared_functions):
            result += '      %4d: %s\n' % (
                number,
                function.__repr__().replace('\n', '\n            ')
            )

        for number, function in enumerate(bytecode.functions):
            result += '      %4d: %s\n' % (
                number,
                function.__repr__().replace('\n', '\n            ')
            )

    result += '\n  Code:\n'
    position = 0
    code = bytecode.code
    code_length = len(code)
    line = 0
    while True:
        if position >= code_length:
            break

        next_instr = ord(code[position])
        result += '      '
        if bytecode.opcode_lines[position] != line:
            line = bytecode.opcode_lines[position]
            result += '%5d   ' % line
        else:
            result += '        '

        result += '%4d ' % (position)

        position += 1
        opcode_name = get_opcode_name(next_instr)
        result += ' %-30s' % opcode_name

        if next_instr > OPCODE_INDEX_DIVIDER:
            arg = ord(code[position]) + (ord(code[position + 1]) << 8)
            position += 2
            result += ' ' + str(arg)

        result += '\n'

    return result