Exemple #1
0
 def disassemble(self, bytecode, currpc=-1):
     self.currpc = currpc
     self.bytecode = bytecode
     code = bytecode.code
     pc = 0
     while pc < len(code):
         self.start(pc)
         opcode = ord(code[pc])
         pc += 1
         if compile.isjump(opcode):
             oparg = self.read4(code, pc)
             pc += 4
         elif compile.hasarg(opcode):
             oparg = ord(code[pc])
             pc += 1
             if oparg >= 128:
                 if oparg > 128:
                     oparg -= 256
                 else:
                     oparg = self.read4(code, pc)
                     pc += 4
         else:
             oparg = 0
         self.pc = pc
         self.end(opcode, oparg)
         name = opcode2name[opcode]
         try:
             method = self.special_methods[name]
             method(self, opcode, oparg)
         except KeyError:
             self.dummy(opcode, oparg)
Exemple #2
0
    def run(self):
        while self.pc < len(self.code.code):
            op = self.next_instruction()

            arg = 0
            if compile.isjump(op):
                arg = self.read_arg4()
            elif compile.hasarg(op):
                arg = self.read_smallarg()

            if self.debug:
                print '(%d) pc:%d op: %d (%s) args %d' % (self.debug_level, self.pc, op, disass.opcode2name[op], arg)
                print disass.disassemble(self.code, '(%d)' % self.debug_level, pc = self.pc)

            if op == compile.INT_LITERAL:
                self.op_int_literal(arg)
            elif op == compile.SET_LOCAL:
                # TODO something is strange
                t = self.stack.pop()
                self.op_implicit_self()
                self.stack.append(t)
                self.op_assignment(arg)
            elif op == compile.PRIMITIVE_METHOD_CALL:
                self.op_primitive_method_call(arg)
            elif op == compile.POP:
                self.stack.pop()
            elif op == compile.GET_LOCAL:
                self.op_implicit_self()
                self.op_method_lookup(arg)
                self.op_method_call(0)
            elif op == compile.JUMP_IF_FALSE:
                self.op_jump_if_false(arg)
            elif op == compile.JUMP:
                self.op_jump(arg)
            elif op == compile.MAKE_OBJECT:
                self.op_make_object(arg)
            elif op == compile.MAKE_OBJECT_CALL:
                self.op_make_object_call(arg)
            elif op == compile.ASSIGNMENT_APPEND_PARENT:
                self.op_assignment_append_parrent(arg)
            elif op == compile.METHOD_LOOKUP:
                self.op_method_lookup(arg)
            elif op == compile.METHOD_CALL:
                self.op_method_call(arg)
            elif op == compile.DUP:
                self.op_dup()
            elif op == compile.ASSIGNMENT:
                self.op_assignment(arg)
            elif op == compile.MAKE_FUNCTION:
                self.op_make_function(arg)
            elif op == compile.IMPLICIT_SELF:
                self.op_implicit_self()
            else:
                raise NotImplementedError(compile.opcode_names[op])
        return self.context