def newarray(frame): atype = ord(frame.code[frame.pc]) frame.pc += 1 count = frame.stack.pop() jassert_int(count) if count < 0: frame.vm.raise_exception(frame, "java/lang/NegativeArraySizeException") return values = None if atype in [10, 5, 8, 9, 4]: # int, char, byte, short, boolean values = [0]*count elif atype == 7: # double values = [("double", 0.0)] * count elif atype == 6: # float values = [("float", 0.0)] * count elif atype == 11: # long values = [("long", 0)] * count else: raise Exception("Array creation for ATYPE {0} not yet supported" .format(atype)) prims = {4: "[Z", 5: "[C", 6: "[F", 7: "[D", 8: "[B", 9: "[S", 10: "[I", 11: "[J"} array_class = frame.vm.get_class(prims[atype]) jarray = JArray(array_class, frame.vm) jarray.values = values ref = frame.vm.add_to_heap(jarray) frame.stack.append(ref)
def iadd(frame): value2 = frame.stack.pop() value1 = frame.stack.pop() result = value1 + value2 result = cut_to_int(result) jassert_int(result) frame.stack.append(result)
def ireturn(frame): value = frame.stack.pop() logger.debug("To be returned {0}".format(value)) jassert_int(value) frame.ret = value frame.has_result = True frame.pc = len(frame.code) + 1
def i2b(frame): value = frame.stack.pop() jassert_int(value) data = struct.pack(">i", value) data = data[3] result = struct.unpack(">b", data)[0] frame.stack.append(result)
def newarray(frame): atype = ord(frame.code[frame.pc]) frame.pc += 1 count = frame.stack.pop() jassert_int(count) if count < 0: frame.vm.raise_exception(frame, "java/lang/NegativeArraySizeException") return values = None if atype in [10, 5, 8, 9, 4]: # int, char, byte, short, boolean values = [0] * count elif atype == 7: # double values = [("double", 0.0)] * count elif atype == 6: # float values = [("float", 0.0)] * count elif atype == 11: # long values = [("long", 0)] * count else: raise Exception( "Array creation for ATYPE {0} not yet supported".format(atype)) prims = { 4: "[Z", 5: "[C", 6: "[F", 7: "[D", 8: "[B", 9: "[S", 10: "[I", 11: "[J" } array_class = frame.vm.get_class(prims[atype]) jarray = JArray(array_class, frame.vm) jarray.values = values ref = frame.vm.add_to_heap(jarray) frame.stack.append(ref)
def op_0x60(frame): # iadd value2 = frame.stack.pop() value1 = frame.stack.pop() result = value1 + value2 result = cut_to_int(result) jassert_int(result) frame.stack.append(result)
def i2s(frame): value = frame.stack.pop() jassert_int(value) data = struct.pack(">i", value) data = data[2:] result = struct.unpack(">h", data)[0] assert type(result) is int frame.stack.append(result)
def l2i(frame): value = frame.stack.pop() jassert_long(value) data = struct.pack(">q", value[1]) data = data[4:] result = struct.unpack(">i", data)[0] jassert_int(result) frame.stack.append(result)
def i2c(frame): value = frame.stack.pop() jassert_int(value) data = struct.pack(">i", value) data = data[2:] result = struct.unpack(">H", data)[0] assert type(result) is int assert 0 <= result <= int(math.pow(2, 16)) frame.stack.append(result)
def op_0x99(frame): # if_eq byte1 = ord(frame.code[frame.pc]) byte2 = ord(frame.code[frame.pc + 1]) frame.pc += 2 offset = struct.unpack(">h", chr(byte1) + chr(byte2))[0] value = frame.stack.pop() jassert_int(value) if value == 0: frame.pc += offset - 2 - 1
def ifle(frame): byte1 = ord(frame.code[frame.pc]) byte2 = ord(frame.code[frame.pc + 1]) frame.pc += 2 offset = struct.unpack(">h", chr(byte1) + chr(byte2))[0] value = frame.stack.pop() jassert_int(value) if value <= 0: frame.pc += offset - 2 - 1
def op_0x79(frame): # lshl value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_long(value1) value2 &= 0b111111 result = value1[1] << value2 result = ("long", cut_to_long(result)) jassert_long(result) frame.stack.append(result)
def lshr(frame): value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_long(value1) value2 &= 0b111111 result = value1[1] >> value2 result = ("long", cut_to_long(result)) jassert_long(result) frame.stack.append(result)
def if_icmpgt(frame): byte1 = ord(frame.code[frame.pc]) byte2 = ord(frame.code[frame.pc + 1]) frame.pc += 2 offset = struct.unpack(">h", chr(byte1) + chr(byte2))[0] value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value1) jassert_int(value2) if value1 > value2: frame.pc += offset - 2 - 1
def op_0xa4(frame): # if_icmple byte1 = ord(frame.code[frame.pc]) byte2 = ord(frame.code[frame.pc + 1]) frame.pc += 2 offset = struct.unpack(">h", chr(byte1) + chr(byte2))[0] value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value1) jassert_int(value2) if value1 <= value2: frame.pc += offset - 2 - 1
def cut_to_int(value): if -2147483648 <= value <= 2147483647: return int(value) if value & FLAG32: value &= 0xFFFFFFFF value ^= 0xFFFFFFFF value += 1 value *= -1 else: value &= 0xFFFFFFFF jassert_int(value) return int(value)
def lushr(frame): value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_long(value1) jassert_int(value2) value2 &= 0b111111 data = struct.pack(">q", value1[1]) result = struct.unpack(">Q", data)[0] result >>= value2 data = struct.pack(">Q", result) result = struct.unpack(">q", data)[0] result = ("long", cut_to_long(result)) jassert_long(result) frame.stack.append(result)
def op_0x8e(frame): # d2i value = frame.stack.pop() jassert_double(value) if value[1] is None: frame.stack.append(0) else: if value[1] < -2147483648: # -1 * math.pow(2, 31) result = -2147483648 elif value[1] > 2147483647: # math.pow(2, 31) - 1 result = 2147483647 else: result = int(value[1]) jassert_int(result) frame.stack.append(result)
def f2i(frame): value = frame.stack.pop() jassert_float(value) if value[1] is None: frame.stack.append(0) else: if value[1] < -2147483648: # -1 * math.pow(2, 31) result = -2147483648 elif value[1] > 2147483647: # math.pow(2, 31) - 1 result = 2147483647 else: result = int(value[1]) jassert_int(result) frame.stack.append(result)
def aaload(frame): index = frame.stack.pop() ref = frame.stack.pop() jassert_int(index) if ref is None: frame.vm.raise_exception(frame, "java/lang/NullPointerException") return jassert_ref(ref) array = frame.vm.heap[ref[1]] values = array.values if index < 0 or index >= len(values): frame.vm.raise_exception(frame, "java/lang/ArrayIndexOutOfBoundsException") return frame.stack.append(values[index])
def sastore(frame): value = frame.stack.pop() index = frame.stack.pop() ref = frame.stack.pop() jassert_int(value) jassert_int(index) if ref is None: frame.vm.raise_exception(frame, "java/lang/NullPointerException") return jassert_ref(ref) array = frame.vm.heap[ref[1]] jassert_array(array) values = array.values if index < 0 or index >= len(values): frame.vm.raise_exception(frame, "java/lang/ArrayIndexOutOfBoundsException") return values[index] = value
def anewarray(frame): index = (ord(frame.code[frame.pc]) << 8) + ord(frame.code[frame.pc + 1]) frame.pc += 2 cp_item = frame.this_class.constant_pool[index] assert cp_item[0] == 7 # CONSTANT_Class klass_name = frame.this_class.constant_pool[cp_item[1]][1] assert type(klass_name) is unicode frame.vm.get_class(klass_name) # make sure it is loaded count = frame.stack.pop() jassert_int(count) if count < 0: frame.vm.raise_exception(frame, "java/lang/NegativeArraySizeException") return values = [None] * count array_class = frame.vm.get_class("[L" + klass_name + ";") jarray = JArray(array_class, frame.vm) jarray.values = values ref = frame.vm.add_to_heap(jarray) frame.stack.append(ref)
def op_0xaa(frame): # tableswitch index = frame.stack.pop() jassert_int(index) last_pc = frame.pc - 1 while frame.pc % 4 != 0: frame.pc += 1 default = struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0] frame.pc += 4 low = struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0] frame.pc += 4 high = struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0] frame.pc += 4 if index < low or index > high: frame.pc = last_pc + default return count = high - low + 1 offsets = [] for i in range(count): offsets.append(struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0]) frame.pc += 4 frame.pc = last_pc + offsets[index - low]
def tableswitch(frame): index = frame.stack.pop() jassert_int(index) last_pc = frame.pc - 1 while frame.pc % 4 != 0: frame.pc += 1 default = struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0] frame.pc += 4 low = struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0] frame.pc += 4 high = struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0] frame.pc += 4 if index < low or index > high: frame.pc = last_pc + default return count = high - low + 1 offsets = [] for i in range(count): offsets.append( struct.unpack(">i", frame.code[frame.pc:frame.pc + 4])[0]) frame.pc += 4 frame.pc = last_pc + offsets[index - low]
def ixor(frame): value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_int(value1) result = value1 ^ value2 result = cut_to_int(result) jassert_int(result) frame.stack.append(result)
def op_0x80(frame): # ior value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_int(value1) result = value1 | value2 result = cut_to_int(result) jassert_int(result) frame.stack.append(result)
def op_0x7e(frame): # iand value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_int(value1) result = value1 & value2 result = cut_to_int(result) jassert_int(result) frame.stack.append(result)
def op_0x78(frame): # ishl value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_int(value1) value2 &= 0b11111 result = value1 << value2 result = cut_to_int(result) jassert_int(result) frame.stack.append(result)
def ishr(frame): value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_int(value1) value2 &= 0b11111 result = value1 >> value2 result = cut_to_int(result) jassert_int(result) frame.stack.append(result)
def iushr(frame): value2 = frame.stack.pop() value1 = frame.stack.pop() jassert_int(value2) jassert_int(value1) value2 &= 0b11111 data = struct.pack(">i", value1) result = struct.unpack(">I", data)[0] result >>= value2 data = struct.pack(">I", result) result = struct.unpack(">i", data)[0] jassert_int(value1) frame.stack.append(result)
def op_0x3e(frame): # istore_3 value = frame.stack.pop() jassert_int(value) frame.args[3] = value
def op_0x3d(frame): # istore_2 value = frame.stack.pop() jassert_int(value) frame.args[2] = value
def op_0x3c(frame): # istore_1 value = frame.stack.pop() jassert_int(value) frame.args[1] = value
def op_0x3b(frame): # istore_0 value = frame.stack.pop() jassert_int(value) frame.args[0] = value
def op_0x36(frame): # istore index = ord(frame.code[frame.pc]) frame.pc += 1 value = frame.stack.pop() jassert_int(value) frame.args[index] = value
def i2d(frame): value = frame.stack.pop() jassert_int(value) frame.stack.append(("double", float(value)))
def i2f(frame): value = frame.stack.pop() jassert_int(value) frame.stack.append(("float", float(value)))
def i2l(frame): value = frame.stack.pop() jassert_int(value) value = long(value) # no real need frame.stack.append(("long", value))
def iload(frame): index = ord(frame.code[frame.pc]) frame.pc += 1 value = frame.args[index] jassert_int(value) frame.stack.append(value)
def iload_3(frame): value = frame.args[3] jassert_int(value) frame.stack.append(value)
def istore_3(frame): value = frame.stack.pop() jassert_int(value) frame.args[3] = value
def istore(frame): index = ord(frame.code[frame.pc]) frame.pc += 1 value = frame.stack.pop() jassert_int(value) frame.args[index] = value