paul@4 | 1 | #!/usr/bin/env python |
paul@4 | 2 | |
paul@4 | 3 | """ |
paul@4 | 4 | Java bytecode conversion. Specification found at the following URL: |
paul@4 | 5 | http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc.html |
paul@5 | 6 | |
paul@5 | 7 | NOTE: Synchronized constructs are not actually supported. |
paul@4 | 8 | """ |
paul@4 | 9 | |
paul@8 | 10 | from dis import opmap, cmp_op # for access to Python bytecode values and operators |
paul@6 | 11 | from UserDict import UserDict |
paul@4 | 12 | |
paul@5 | 13 | # Bytecode production classes. |
paul@5 | 14 | |
paul@5 | 15 | class BytecodeWriter: |
paul@7 | 16 | |
paul@7 | 17 | "A Python bytecode writer." |
paul@7 | 18 | |
paul@5 | 19 | def __init__(self): |
paul@5 | 20 | self.loops = [] |
paul@6 | 21 | self.jumps = {} |
paul@5 | 22 | self.output = [] |
paul@5 | 23 | self.position = 0 |
paul@5 | 24 | |
paul@8 | 25 | # Stack depth estimation. |
paul@8 | 26 | self.stack_depth = 0 |
paul@8 | 27 | self.max_stack_depth = 0 |
paul@8 | 28 | |
paul@6 | 29 | # Mapping from values to indexes. |
paul@6 | 30 | self.constants = {} |
paul@6 | 31 | |
paul@6 | 32 | # Mapping from names to indexes. |
paul@6 | 33 | # NOTE: This may be acquired from elsewhere. |
paul@8 | 34 | #self.globals = {} |
paul@6 | 35 | |
paul@7 | 36 | # Mapping from names to indexes. |
paul@7 | 37 | self.names = {} |
paul@7 | 38 | |
paul@8 | 39 | # A list of constants used as exception handler return addresses. |
paul@8 | 40 | self.constants_for_exceptions = [] |
paul@8 | 41 | |
paul@6 | 42 | def get_output(self): |
paul@6 | 43 | output = [] |
paul@6 | 44 | for element in self.output: |
paul@8 | 45 | if isinstance(element, LazySubValue): |
paul@8 | 46 | value = element.value |
paul@6 | 47 | else: |
paul@8 | 48 | value = element |
paul@8 | 49 | output.append(chr(value)) |
paul@6 | 50 | return "".join(output) |
paul@6 | 51 | |
paul@8 | 52 | def get_constants(self): |
paul@8 | 53 | l = self._get_list(self._invert(self.constants)) |
paul@8 | 54 | result = [] |
paul@8 | 55 | for i in l: |
paul@8 | 56 | if isinstance(i, LazyValue): |
paul@8 | 57 | result.append(i.get_value()) |
paul@8 | 58 | else: |
paul@8 | 59 | result.append(i) |
paul@8 | 60 | return result |
paul@8 | 61 | |
paul@8 | 62 | #def get_globals(self): |
paul@8 | 63 | # return self._get_list(self._invert(self.globals)) |
paul@8 | 64 | |
paul@8 | 65 | def get_names(self): |
paul@8 | 66 | return self._get_list(self._invert(self.names)) |
paul@8 | 67 | |
paul@8 | 68 | def _invert(self, d): |
paul@8 | 69 | inverted = {} |
paul@8 | 70 | for k, v in d.items(): |
paul@8 | 71 | inverted[v] = k |
paul@8 | 72 | return inverted |
paul@8 | 73 | |
paul@8 | 74 | def _get_list(self, d): |
paul@8 | 75 | l = [] |
paul@8 | 76 | for i in range(0, len(d.keys())): |
paul@8 | 77 | l.append(d[i]) |
paul@8 | 78 | return l |
paul@8 | 79 | |
paul@8 | 80 | # Administrative methods. |
paul@8 | 81 | |
paul@8 | 82 | def update_stack_depth(self, change): |
paul@8 | 83 | self.stack_depth += change |
paul@8 | 84 | if self.stack_depth > self.max_stack_depth: |
paul@8 | 85 | self.max_stack_depth = self.stack_depth |
paul@8 | 86 | |
paul@5 | 87 | # Special methods. |
paul@5 | 88 | |
paul@8 | 89 | def _write_value(self, value): |
paul@8 | 90 | if isinstance(value, LazyValue): |
paul@8 | 91 | # NOTE: Assume a 16-bit value. |
paul@8 | 92 | self.output.append(value.values[0]) |
paul@8 | 93 | self.output.append(value.values[1]) |
paul@8 | 94 | elif value <= 0xffff: |
paul@8 | 95 | self.output.append(value & 0xff) |
paul@8 | 96 | self.output.append((value & 0xff00) >> 8) |
paul@8 | 97 | self.position += 2 |
paul@8 | 98 | else: |
paul@8 | 99 | # NOTE: EXTENDED_ARG not yet supported. |
paul@8 | 100 | raise ValueError, value |
paul@8 | 101 | |
paul@9 | 102 | def setup_loop(self): |
paul@9 | 103 | self.loops.push(self.position) |
paul@9 | 104 | self.output.append(opmap["SETUP_LOOP"]) |
paul@9 | 105 | self.position += 1 |
paul@9 | 106 | self._write_value(0) # To be filled in later |
paul@9 | 107 | |
paul@5 | 108 | def end_loop(self): |
paul@5 | 109 | current_loop_start = self.loops.pop() |
paul@5 | 110 | self.jump_absolute(current_loop_start) |
paul@5 | 111 | self.output[current_loop_start + 1] = self.position |
paul@5 | 112 | self.pop_block() |
paul@5 | 113 | |
paul@6 | 114 | def jump_to_label(self, status, name): |
paul@6 | 115 | # Record the instruction using the jump. |
paul@8 | 116 | jump_instruction = self.position |
paul@6 | 117 | if status is None: |
paul@6 | 118 | self.jump_forward() |
paul@6 | 119 | elif status: |
paul@5 | 120 | self.jump_if_true() |
paul@5 | 121 | else: |
paul@5 | 122 | self.jump_if_false() |
paul@8 | 123 | # Record the following instruction, too. |
paul@8 | 124 | if not self.jumps.has_key(name): |
paul@8 | 125 | self.jumps[name] = [] |
paul@8 | 126 | self.jumps[name].append((jump_instruction, self.position)) |
paul@5 | 127 | |
paul@6 | 128 | def start_label(self, name): |
paul@6 | 129 | # Fill in all jump instructions. |
paul@8 | 130 | for jump_instruction, following_instruction in self.jumps[name]: |
paul@8 | 131 | self.output[jump_instruction + 1] = self.position - following_instruction |
paul@8 | 132 | del self.jumps[name] |
paul@8 | 133 | |
paul@8 | 134 | def load_const_ret(self, value): |
paul@8 | 135 | self.constants_for_exceptions.append(value) |
paul@8 | 136 | self.load_const(value) |
paul@8 | 137 | |
paul@8 | 138 | def ret(self, index): |
paul@8 | 139 | # Previously, the constant stored on the stack by jsr/jsr_w was stored |
paul@8 | 140 | # in a local variable. In the JVM, extracting the value from the local |
paul@8 | 141 | # variable and jumping can be done at runtime. In the Python VM, any |
paul@8 | 142 | # jump target must be known in advance and written into the bytecode. |
paul@8 | 143 | self.load_fast(index) |
paul@8 | 144 | for constant in self.constants_for_exceptions: |
paul@8 | 145 | self.dup_top() # Stack: actual-address, actual-address |
paul@8 | 146 | self.load_const(constant) # Stack: actual-address, actual-address, suggested-address |
paul@8 | 147 | self.compare_op("==") # Stack: actual-address, result |
paul@8 | 148 | self.jump_to_label(0, "const") |
paul@8 | 149 | self.jump_absolute(constant) |
paul@8 | 150 | self.start_label("const") |
paul@8 | 151 | self.pop_top() # Stack: actual-address |
paul@6 | 152 | |
paul@6 | 153 | # Complicated methods. |
paul@6 | 154 | |
paul@6 | 155 | def load_const(self, value): |
paul@6 | 156 | self.output.append(opmap["LOAD_CONST"]) |
paul@6 | 157 | if not self.constants.has_key(value): |
paul@6 | 158 | self.constants[value] = len(self.constants.keys()) |
paul@8 | 159 | self.position += 1 |
paul@8 | 160 | self._write_value(self.constants[value]) |
paul@8 | 161 | self.update_stack_depth(1) |
paul@6 | 162 | |
paul@6 | 163 | def load_global(self, name): |
paul@6 | 164 | self.output.append(opmap["LOAD_GLOBAL"]) |
paul@8 | 165 | if not self.names.has_key(name): |
paul@8 | 166 | self.names[name] = len(self.names.keys()) |
paul@8 | 167 | self.position += 1 |
paul@8 | 168 | self._write_value(self.names[name]) |
paul@8 | 169 | self.update_stack_depth(1) |
paul@7 | 170 | |
paul@7 | 171 | def load_attr(self, name): |
paul@7 | 172 | self.output.append(opmap["LOAD_ATTR"]) |
paul@7 | 173 | if not self.names.has_key(name): |
paul@7 | 174 | self.names[name] = len(self.names.keys()) |
paul@8 | 175 | self.position += 1 |
paul@8 | 176 | self._write_value(self.names[name]) |
paul@8 | 177 | |
paul@8 | 178 | def load_name(self, name): |
paul@8 | 179 | self.output.append(opmap["LOAD_NAME"]) |
paul@8 | 180 | if not self.names.has_key(name): |
paul@8 | 181 | self.names[name] = len(self.names.keys()) |
paul@8 | 182 | self.position += 1 |
paul@8 | 183 | self._write_value(self.names[name]) |
paul@8 | 184 | self.update_stack_depth(1) |
paul@6 | 185 | |
paul@6 | 186 | def load_fast(self, index): |
paul@6 | 187 | self.output.append(opmap["LOAD_FAST"]) |
paul@8 | 188 | self.position += 1 |
paul@8 | 189 | self._write_value(index) |
paul@8 | 190 | self.update_stack_depth(1) |
paul@8 | 191 | |
paul@8 | 192 | def store_attr(self, name): |
paul@8 | 193 | self.output.append(opmap["STORE_ATTR"]) |
paul@8 | 194 | if not self.names.has_key(name): |
paul@8 | 195 | self.names[name] = len(self.names.keys()) |
paul@8 | 196 | self.position += 1 |
paul@8 | 197 | self._write_value(self.names[name]) |
paul@8 | 198 | self.update_stack_depth(-1) |
paul@8 | 199 | |
paul@8 | 200 | def store_fast(self, index): |
paul@8 | 201 | self.output.append(opmap["STORE_FAST"]) |
paul@8 | 202 | self.position += 1 |
paul@8 | 203 | self._write_value(index) |
paul@8 | 204 | self.update_stack_depth(-1) |
paul@5 | 205 | |
paul@5 | 206 | # Normal bytecode generators. |
paul@5 | 207 | |
paul@5 | 208 | def for_iter(self): |
paul@5 | 209 | self.loops.push(self.position) |
paul@5 | 210 | self.output.append(opmap["FOR_ITER"]) |
paul@8 | 211 | self.position += 1 |
paul@8 | 212 | self._write_value(0) # To be filled in later |
paul@8 | 213 | self.update_stack_depth(1) |
paul@5 | 214 | |
paul@8 | 215 | def jump_if_false(self, offset=0): |
paul@5 | 216 | self.output.append(opmap["JUMP_IF_FALSE"]) |
paul@8 | 217 | self.position += 1 |
paul@8 | 218 | self._write_value(offset) # May be filled in later |
paul@5 | 219 | |
paul@8 | 220 | def jump_if_true(self, offset=0): |
paul@5 | 221 | self.output.append(opmap["JUMP_IF_TRUE"]) |
paul@8 | 222 | self.position += 1 |
paul@8 | 223 | self._write_value(offset) # May be filled in later |
paul@5 | 224 | |
paul@8 | 225 | def jump_forward(self, offset=0): |
paul@6 | 226 | self.output.append(opmap["JUMP_FORWARD"]) |
paul@8 | 227 | self.position += 1 |
paul@8 | 228 | self._write_value(offset) # May be filled in later |
paul@8 | 229 | |
paul@8 | 230 | def jump_absolute(self, address=0): |
paul@8 | 231 | self.output.append(opmap["JUMP_ABSOLUTE"]) |
paul@8 | 232 | self.position += 1 |
paul@8 | 233 | self._write_value(address) # May be filled in later |
paul@6 | 234 | |
paul@7 | 235 | def build_tuple(self, count): |
paul@7 | 236 | self.output.append(opmap["BUILD_TUPLE"]) |
paul@8 | 237 | self.position += 1 |
paul@8 | 238 | self._write_value(count) |
paul@8 | 239 | self.update_stack_depth(-(count - 1)) |
paul@8 | 240 | |
paul@8 | 241 | def build_list(self, count): |
paul@8 | 242 | self.output.append(opmap["BUILD_LIST"]) |
paul@8 | 243 | self.position += 1 |
paul@8 | 244 | self._write_value(count) |
paul@8 | 245 | self.update_stack_depth(-(count - 1)) |
paul@8 | 246 | |
paul@8 | 247 | def pop_top(self): |
paul@8 | 248 | self.output.append(opmap["POP_TOP"]) |
paul@8 | 249 | self.position += 1 |
paul@8 | 250 | self.update_stack_depth(-1) |
paul@8 | 251 | |
paul@8 | 252 | def dup_top(self): |
paul@8 | 253 | self.output.append(opmap["DUP_TOP"]) |
paul@8 | 254 | self.position += 1 |
paul@8 | 255 | self.update_stack_depth(1) |
paul@7 | 256 | |
paul@7 | 257 | def rot_two(self): |
paul@7 | 258 | self.output.append(opmap["ROT_TWO"]) |
paul@7 | 259 | self.position += 1 |
paul@7 | 260 | |
paul@7 | 261 | def rot_three(self): |
paul@7 | 262 | self.output.append(opmap["ROT_THREE"]) |
paul@7 | 263 | self.position += 1 |
paul@7 | 264 | |
paul@7 | 265 | def rot_four(self): |
paul@7 | 266 | self.output.append(opmap["ROT_FOUR"]) |
paul@7 | 267 | self.position += 1 |
paul@7 | 268 | |
paul@7 | 269 | def call_function(self, count): |
paul@7 | 270 | self.output.append(opmap["CALL_FUNCTION"]) |
paul@8 | 271 | self.position += 1 |
paul@8 | 272 | self._write_value(count) |
paul@8 | 273 | self.update_stack_depth(-count) |
paul@8 | 274 | |
paul@8 | 275 | def binary_subscr(self): |
paul@8 | 276 | self.output.append(opmap["BINARY_SUBSCR"]) |
paul@8 | 277 | self.position += 1 |
paul@8 | 278 | self.update_stack_depth(-1) |
paul@8 | 279 | |
paul@8 | 280 | def binary_add(self): |
paul@8 | 281 | self.output.append(opmap["BINARY_ADD"]) |
paul@8 | 282 | self.position += 1 |
paul@8 | 283 | self.update_stack_depth(-1) |
paul@8 | 284 | |
paul@8 | 285 | def binary_divide(self): |
paul@8 | 286 | self.output.append(opmap["BINARY_DIVIDE"]) |
paul@8 | 287 | self.position += 1 |
paul@8 | 288 | self.update_stack_depth(-1) |
paul@8 | 289 | |
paul@8 | 290 | def binary_multiply(self): |
paul@8 | 291 | self.output.append(opmap["BINARY_MULTIPLY"]) |
paul@8 | 292 | self.position += 1 |
paul@8 | 293 | self.update_stack_depth(-1) |
paul@8 | 294 | |
paul@8 | 295 | def binary_modulo(self): |
paul@8 | 296 | self.output.append(opmap["BINARY_MODULO"]) |
paul@8 | 297 | self.position += 1 |
paul@8 | 298 | self.update_stack_depth(-1) |
paul@8 | 299 | |
paul@8 | 300 | def binary_subtract(self): |
paul@8 | 301 | self.output.append(opmap["BINARY_SUBTRACT"]) |
paul@8 | 302 | self.position += 1 |
paul@8 | 303 | self.update_stack_depth(-1) |
paul@8 | 304 | |
paul@8 | 305 | def binary_and(self): |
paul@8 | 306 | self.output.append(opmap["BINARY_AND"]) |
paul@8 | 307 | self.position += 1 |
paul@8 | 308 | self.update_stack_depth(-1) |
paul@8 | 309 | |
paul@8 | 310 | def binary_or(self): |
paul@8 | 311 | self.output.append(opmap["BINARY_XOR"]) |
paul@8 | 312 | self.position += 1 |
paul@8 | 313 | self.update_stack_depth(-1) |
paul@8 | 314 | |
paul@8 | 315 | def binary_lshift(self): |
paul@8 | 316 | self.output.append(opmap["BINARY_LSHIFT"]) |
paul@8 | 317 | self.position += 1 |
paul@8 | 318 | self.update_stack_depth(-1) |
paul@8 | 319 | |
paul@8 | 320 | def binary_rshift(self): |
paul@8 | 321 | self.output.append(opmap["BINARY_RSHIFT"]) |
paul@8 | 322 | self.position += 1 |
paul@8 | 323 | self.update_stack_depth(-1) |
paul@8 | 324 | |
paul@8 | 325 | def binary_xor(self): |
paul@8 | 326 | self.output.append(opmap["BINARY_XOR"]) |
paul@8 | 327 | self.position += 1 |
paul@8 | 328 | self.update_stack_depth(-1) |
paul@8 | 329 | |
paul@8 | 330 | def compare_op(self, op): |
paul@8 | 331 | self.output.append(opmap["COMPARE_OP"]) |
paul@8 | 332 | self.position += 1 |
paul@8 | 333 | self._write_value(list(cmp_op).index(op)) |
paul@8 | 334 | self.update_stack_depth(-1) |
paul@8 | 335 | |
paul@8 | 336 | def return_value(self): |
paul@8 | 337 | self.output.append(opmap["RETURN_VALUE"]) |
paul@8 | 338 | self.position += 1 |
paul@8 | 339 | self.update_stack_depth(-1) |
paul@8 | 340 | |
paul@8 | 341 | def raise_varargs(self, count): |
paul@8 | 342 | self.output.append(opmap["RAISE_VARARGS"]) |
paul@8 | 343 | self.position += 1 |
paul@8 | 344 | self._write_value(count) |
paul@7 | 345 | |
paul@9 | 346 | def pop_block(self): |
paul@9 | 347 | self.output.append(opmap["POP_BLOCK"]) |
paul@9 | 348 | self.position += 1 |
paul@9 | 349 | |
paul@6 | 350 | # Utility classes and functions. |
paul@6 | 351 | |
paul@6 | 352 | class LazyDict(UserDict): |
paul@6 | 353 | def __getitem__(self, key): |
paul@6 | 354 | if not self.data.has_key(key): |
paul@8 | 355 | # NOTE: Assume 16-bit value. |
paul@8 | 356 | self.data[key] = LazyValue(2) |
paul@6 | 357 | return self.data[key] |
paul@6 | 358 | def __setitem__(self, key, value): |
paul@6 | 359 | if self.data.has_key(key): |
paul@6 | 360 | existing_value = self.data[key] |
paul@6 | 361 | if isinstance(existing_value, LazyValue): |
paul@8 | 362 | existing_value.set_value(value) |
paul@6 | 363 | return |
paul@6 | 364 | self.data[key] = value |
paul@6 | 365 | |
paul@6 | 366 | class LazyValue: |
paul@8 | 367 | def __init__(self, nvalues): |
paul@8 | 368 | self.values = [] |
paul@8 | 369 | for i in range(0, nvalues): |
paul@8 | 370 | self.values.append(LazySubValue()) |
paul@8 | 371 | def set_value(self, value): |
paul@8 | 372 | # NOTE: Assume at least 16-bit value. No "filling" performed. |
paul@8 | 373 | if value <= 0xffff: |
paul@8 | 374 | self.values[0].set_value(value & 0xff) |
paul@8 | 375 | self.values[1].set_value((value & 0xff00) >> 8) |
paul@8 | 376 | else: |
paul@8 | 377 | # NOTE: EXTENDED_ARG not yet supported. |
paul@8 | 378 | raise ValueError, value |
paul@8 | 379 | def get_value(self): |
paul@8 | 380 | value = 0 |
paul@8 | 381 | for i in range(0, len(self.values)): |
paul@8 | 382 | value = (value << 8) + self.values.pop().value |
paul@8 | 383 | return value |
paul@8 | 384 | |
paul@8 | 385 | class LazySubValue: |
paul@8 | 386 | def __init__(self): |
paul@8 | 387 | self.value = 0 |
paul@8 | 388 | def set_value(self, value): |
paul@6 | 389 | self.value = value |
paul@6 | 390 | |
paul@6 | 391 | def signed(value, limit): |
paul@6 | 392 | |
paul@6 | 393 | """ |
paul@6 | 394 | Return the signed integer from the unsigned 'value', where 'limit' (a value |
paul@6 | 395 | one greater than the highest possible positive integer) is used to determine |
paul@6 | 396 | whether a negative or positive result is produced. |
paul@6 | 397 | """ |
paul@6 | 398 | |
paul@6 | 399 | d, r = divmod(value, limit) |
paul@6 | 400 | if d == 1: |
paul@6 | 401 | mask = limit * 2 - 1 |
paul@6 | 402 | return -1 - (value ^ mask) |
paul@6 | 403 | else: |
paul@6 | 404 | return value |
paul@6 | 405 | |
paul@6 | 406 | def signed2(value): |
paul@6 | 407 | return signed(value, 0x8000) |
paul@6 | 408 | |
paul@6 | 409 | def signed4(value): |
paul@6 | 410 | return signed(value, 0x80000000) |
paul@6 | 411 | |
paul@4 | 412 | # Bytecode conversion. |
paul@4 | 413 | |
paul@5 | 414 | class BytecodeReader: |
paul@7 | 415 | |
paul@7 | 416 | "A generic Java bytecode reader." |
paul@7 | 417 | |
paul@5 | 418 | def __init__(self, class_file): |
paul@5 | 419 | self.class_file = class_file |
paul@6 | 420 | self.position_mapping = LazyDict() |
paul@5 | 421 | |
paul@5 | 422 | def process(self, code, program): |
paul@5 | 423 | self.java_position = 0 |
paul@5 | 424 | while self.java_position < len(code): |
paul@5 | 425 | self.position_mapping[self.java_position] = program.position |
paul@5 | 426 | bytecode = ord(code[self.java_position]) |
paul@5 | 427 | mnemonic, number_of_arguments = self.java_bytecodes[bytecode] |
paul@7 | 428 | self.process_bytecode(mnemonic, number_of_arguments, code, program) |
paul@5 | 429 | |
paul@7 | 430 | def process_bytecode(self, mnemonic, number_of_arguments, code, program): |
paul@5 | 431 | if number_of_arguments is not None: |
paul@5 | 432 | arguments = [] |
paul@5 | 433 | for j in range(0, number_of_arguments): |
paul@5 | 434 | arguments.append(ord(code[self.java_position + 1 + j])) |
paul@5 | 435 | |
paul@5 | 436 | # Call the handler. |
paul@5 | 437 | getattr(self, mnemonic)(arguments, program) |
paul@5 | 438 | else: |
paul@5 | 439 | # Call the handler. |
paul@5 | 440 | number_of_arguments = getattr(self, mnemonic)(code[self.java_position+1:], program) |
paul@5 | 441 | |
paul@5 | 442 | self.java_position = self.java_position + 1 + number_of_arguments |
paul@5 | 443 | |
paul@5 | 444 | java_bytecodes = { |
paul@5 | 445 | # code : (mnemonic, number of following bytes, change in stack) |
paul@5 | 446 | 0 : ("nop", 0), |
paul@5 | 447 | 1 : ("aconst_null", 0), |
paul@5 | 448 | 2 : ("iconst_m1", 0), |
paul@5 | 449 | 3 : ("iconst_0", 0), |
paul@5 | 450 | 4 : ("iconst_1", 0), |
paul@5 | 451 | 5 : ("iconst_2", 0), |
paul@5 | 452 | 6 : ("iconst_3", 0), |
paul@5 | 453 | 7 : ("iconst_4", 0), |
paul@5 | 454 | 8 : ("iconst_5", 0), |
paul@5 | 455 | 9 : ("lconst_0", 0), |
paul@5 | 456 | 10 : ("lconst_1", 0), |
paul@5 | 457 | 11 : ("fconst_0", 0), |
paul@5 | 458 | 12 : ("fconst_1", 0), |
paul@5 | 459 | 13 : ("fconst_2", 0), |
paul@5 | 460 | 14 : ("dconst_0", 0), |
paul@5 | 461 | 15 : ("dconst_1", 0), |
paul@5 | 462 | 16 : ("bipush", 1), |
paul@5 | 463 | 17 : ("sipush", 2), |
paul@5 | 464 | 18 : ("ldc", 1), |
paul@5 | 465 | 19 : ("ldc_w", 2), |
paul@5 | 466 | 20 : ("ldc2_w", 2), |
paul@5 | 467 | 21 : ("iload", 1), |
paul@5 | 468 | 22 : ("lload", 1), |
paul@5 | 469 | 23 : ("fload", 1), |
paul@5 | 470 | 24 : ("dload", 1), |
paul@5 | 471 | 25 : ("aload", 1), |
paul@5 | 472 | 26 : ("iload_0", 0), |
paul@5 | 473 | 27 : ("iload_1", 0), |
paul@5 | 474 | 28 : ("iload_2", 0), |
paul@5 | 475 | 29 : ("iload_3", 0), |
paul@5 | 476 | 30 : ("lload_0", 0), |
paul@5 | 477 | 31 : ("lload_1", 0), |
paul@5 | 478 | 32 : ("lload_2", 0), |
paul@5 | 479 | 33 : ("lload_3", 0), |
paul@5 | 480 | 34 : ("fload_0", 0), |
paul@5 | 481 | 35 : ("fload_1", 0), |
paul@5 | 482 | 36 : ("fload_2", 0), |
paul@5 | 483 | 37 : ("fload_3", 0), |
paul@5 | 484 | 38 : ("dload_0", 0), |
paul@5 | 485 | 39 : ("dload_1", 0), |
paul@5 | 486 | 40 : ("dload_2", 0), |
paul@5 | 487 | 41 : ("dload_3", 0), |
paul@5 | 488 | 42 : ("aload_0", 0), |
paul@5 | 489 | 43 : ("aload_1", 0), |
paul@5 | 490 | 44 : ("aload_2", 0), |
paul@5 | 491 | 45 : ("aload_3", 0), |
paul@5 | 492 | 46 : ("iaload", 0), |
paul@5 | 493 | 47 : ("laload", 0), |
paul@5 | 494 | 48 : ("faload", 0), |
paul@5 | 495 | 49 : ("daload", 0), |
paul@5 | 496 | 50 : ("aaload", 0), |
paul@5 | 497 | 51 : ("baload", 0), |
paul@5 | 498 | 52 : ("caload", 0), |
paul@5 | 499 | 53 : ("saload", 0), |
paul@5 | 500 | 54 : ("istore", 1), |
paul@5 | 501 | 55 : ("lstore", 1), |
paul@5 | 502 | 56 : ("fstore", 1), |
paul@5 | 503 | 57 : ("dstore", 1), |
paul@5 | 504 | 58 : ("astore", 1), |
paul@5 | 505 | 59 : ("istore_0", 0), |
paul@5 | 506 | 60 : ("istore_1", 0), |
paul@5 | 507 | 61 : ("istore_2", 0), |
paul@5 | 508 | 62 : ("istore_3", 0), |
paul@5 | 509 | 63 : ("lstore_0", 0), |
paul@5 | 510 | 64 : ("lstore_1", 0), |
paul@5 | 511 | 65 : ("lstore_2", 0), |
paul@5 | 512 | 66 : ("lstore_3", 0), |
paul@5 | 513 | 67 : ("fstore_0", 0), |
paul@5 | 514 | 68 : ("fstore_1", 0), |
paul@5 | 515 | 69 : ("fstore_2", 0), |
paul@5 | 516 | 70 : ("fstore_3", 0), |
paul@5 | 517 | 71 : ("dstore_0", 0), |
paul@5 | 518 | 72 : ("dstore_1", 0), |
paul@5 | 519 | 73 : ("dstore_2", 0), |
paul@5 | 520 | 74 : ("dstore_3", 0), |
paul@5 | 521 | 75 : ("astore_0", 0), |
paul@5 | 522 | 76 : ("astore_1", 0), |
paul@5 | 523 | 77 : ("astore_2", 0), |
paul@5 | 524 | 78 : ("astore_3", 0), |
paul@5 | 525 | 79 : ("iastore", 0), |
paul@5 | 526 | 80 : ("lastore", 0), |
paul@5 | 527 | 81 : ("fastore", 0), |
paul@5 | 528 | 82 : ("dastore", 0), |
paul@5 | 529 | 83 : ("aastore", 0), |
paul@5 | 530 | 84 : ("bastore", 0), |
paul@5 | 531 | 85 : ("castore", 0), |
paul@5 | 532 | 86 : ("sastore", 0), |
paul@5 | 533 | 87 : ("pop", 0), |
paul@5 | 534 | 88 : ("pop2", 0), |
paul@5 | 535 | 89 : ("dup", 0), |
paul@5 | 536 | 90 : ("dup_x1", 0), |
paul@5 | 537 | 91 : ("dup_x2", 0), |
paul@5 | 538 | 92 : ("dup2", 0), |
paul@5 | 539 | 93 : ("dup2_x1", 0), |
paul@5 | 540 | 94 : ("dup2_x2", 0), |
paul@5 | 541 | 95 : ("swap", 0), |
paul@5 | 542 | 96 : ("iadd", 0), |
paul@5 | 543 | 97 : ("ladd", 0), |
paul@5 | 544 | 98 : ("fadd", 0), |
paul@5 | 545 | 99 : ("dadd", 0), |
paul@5 | 546 | 100 : ("isub", 0), |
paul@5 | 547 | 101 : ("lsub", 0), |
paul@5 | 548 | 102 : ("fsub", 0), |
paul@5 | 549 | 103 : ("dsub", 0), |
paul@5 | 550 | 104 : ("imul", 0), |
paul@5 | 551 | 105 : ("lmul", 0), |
paul@5 | 552 | 106 : ("fmul", 0), |
paul@5 | 553 | 107 : ("dmul", 0), |
paul@5 | 554 | 108 : ("idiv", 0), |
paul@5 | 555 | 109 : ("ldiv", 0), |
paul@5 | 556 | 110 : ("fdiv", 0), |
paul@5 | 557 | 111 : ("ddiv", 0), |
paul@5 | 558 | 112 : ("irem", 0), |
paul@5 | 559 | 113 : ("lrem", 0), |
paul@5 | 560 | 114 : ("frem", 0), |
paul@5 | 561 | 115 : ("drem", 0), |
paul@5 | 562 | 116 : ("ineg", 0), |
paul@5 | 563 | 117 : ("lneg", 0), |
paul@5 | 564 | 118 : ("fneg", 0), |
paul@5 | 565 | 119 : ("dneg", 0), |
paul@5 | 566 | 120 : ("ishl", 0), |
paul@5 | 567 | 121 : ("lshl", 0), |
paul@5 | 568 | 122 : ("ishr", 0), |
paul@5 | 569 | 123 : ("lshr", 0), |
paul@5 | 570 | 124 : ("iushr", 0), |
paul@5 | 571 | 125 : ("lushr", 0), |
paul@5 | 572 | 126 : ("iand", 0), |
paul@5 | 573 | 127 : ("land", 0), |
paul@5 | 574 | 128 : ("ior", 0), |
paul@5 | 575 | 129 : ("lor", 0), |
paul@5 | 576 | 130 : ("ixor", 0), |
paul@5 | 577 | 131 : ("lxor", 0), |
paul@5 | 578 | 132 : ("iinc", 2), |
paul@5 | 579 | 133 : ("i2l", 0), |
paul@5 | 580 | 134 : ("i2f", 0), |
paul@5 | 581 | 135 : ("i2d", 0), |
paul@5 | 582 | 136 : ("l2i", 0), |
paul@5 | 583 | 137 : ("l2f", 0), |
paul@5 | 584 | 138 : ("l2d", 0), |
paul@5 | 585 | 139 : ("f2i", 0), |
paul@5 | 586 | 140 : ("f2l", 0), |
paul@5 | 587 | 141 : ("f2d", 0), |
paul@5 | 588 | 142 : ("d2i", 0), |
paul@5 | 589 | 143 : ("d2l", 0), |
paul@5 | 590 | 144 : ("d2f", 0), |
paul@5 | 591 | 145 : ("i2b", 0), |
paul@5 | 592 | 146 : ("i2c", 0), |
paul@5 | 593 | 147 : ("i2s", 0), |
paul@5 | 594 | 148 : ("lcmp", 0), |
paul@5 | 595 | 149 : ("fcmpl", 0), |
paul@5 | 596 | 150 : ("fcmpg", 0), |
paul@5 | 597 | 151 : ("dcmpl", 0), |
paul@5 | 598 | 152 : ("dcmpg", 0), |
paul@5 | 599 | 153 : ("ifeq", 2), |
paul@5 | 600 | 154 : ("ifne", 2), |
paul@5 | 601 | 155 : ("iflt", 2), |
paul@5 | 602 | 156 : ("ifge", 2), |
paul@5 | 603 | 157 : ("ifgt", 2), |
paul@5 | 604 | 158 : ("ifle", 2), |
paul@5 | 605 | 159 : ("if_icmpeq", 2), |
paul@5 | 606 | 160 : ("if_icmpne", 2), |
paul@5 | 607 | 161 : ("if_icmplt", 2), |
paul@5 | 608 | 162 : ("if_icmpge", 2), |
paul@5 | 609 | 163 : ("if_icmpgt", 2), |
paul@5 | 610 | 164 : ("if_icmple", 2), |
paul@5 | 611 | 165 : ("if_acmpeq", 2), |
paul@5 | 612 | 166 : ("if_acmpne", 2), |
paul@5 | 613 | 167 : ("goto", 2), |
paul@5 | 614 | 168 : ("jsr", 2), |
paul@5 | 615 | 169 : ("ret", 1), |
paul@5 | 616 | 170 : ("tableswitch", None), # variable number of arguments |
paul@5 | 617 | 171 : ("lookupswitch", None), # variable number of arguments |
paul@5 | 618 | 172 : ("ireturn", 0), |
paul@5 | 619 | 173 : ("lreturn", 0), |
paul@5 | 620 | 174 : ("freturn", 0), |
paul@5 | 621 | 175 : ("dreturn", 0), |
paul@5 | 622 | 176 : ("areturn", 0), |
paul@8 | 623 | 177 : ("return_", 0), |
paul@5 | 624 | 178 : ("getstatic", 2), |
paul@5 | 625 | 179 : ("putstatic", 2), |
paul@5 | 626 | 180 : ("getfield", 2), |
paul@5 | 627 | 181 : ("putfield", 2), |
paul@5 | 628 | 182 : ("invokevirtual", 2), |
paul@5 | 629 | 183 : ("invokespecial", 2), |
paul@5 | 630 | 184 : ("invokestatic", 2), |
paul@5 | 631 | 185 : ("invokeinterface", 4), |
paul@5 | 632 | 187 : ("new", 2), |
paul@5 | 633 | 188 : ("newarray", 1), |
paul@5 | 634 | 189 : ("anewarray", 2), |
paul@5 | 635 | 190 : ("arraylength", 0), |
paul@5 | 636 | 191 : ("athrow", 0), |
paul@5 | 637 | 192 : ("checkcast", 2), |
paul@5 | 638 | 193 : ("instanceof", 2), |
paul@5 | 639 | 194 : ("monitorenter", 0), |
paul@5 | 640 | 195 : ("monitorexit", 0), |
paul@5 | 641 | 196 : ("wide", None), # 3 or 5 arguments, stack changes according to modified element |
paul@5 | 642 | 197 : ("multianewarray", 3), |
paul@5 | 643 | 198 : ("ifnull", 2), |
paul@5 | 644 | 199 : ("ifnonnull", 2), |
paul@5 | 645 | 200 : ("goto_w", 4), |
paul@5 | 646 | 201 : ("jsr_w", 4), |
paul@5 | 647 | } |
paul@5 | 648 | |
paul@7 | 649 | class BytecodeDisassembler(BytecodeReader): |
paul@7 | 650 | |
paul@7 | 651 | "A Java bytecode disassembler." |
paul@7 | 652 | |
paul@7 | 653 | bytecode_methods = [spec[0] for spec in BytecodeReader.java_bytecodes.values()] |
paul@7 | 654 | |
paul@7 | 655 | def __getattr__(self, name): |
paul@7 | 656 | if name in self.bytecode_methods: |
paul@7 | 657 | print name, |
paul@7 | 658 | return self.generic |
paul@7 | 659 | else: |
paul@7 | 660 | raise AttributeError, name |
paul@7 | 661 | |
paul@7 | 662 | def generic(self, arguments, program): |
paul@7 | 663 | print arguments |
paul@7 | 664 | |
paul@7 | 665 | class BytecodeDisassemblerProgram: |
paul@7 | 666 | position = 0 |
paul@7 | 667 | |
paul@7 | 668 | class BytecodeTranslator(BytecodeReader): |
paul@7 | 669 | |
paul@7 | 670 | "A Java bytecode translator which uses a Python bytecode writer." |
paul@7 | 671 | |
paul@7 | 672 | def aaload(self, arguments, program): |
paul@7 | 673 | # NOTE: No type checking performed. |
paul@7 | 674 | program.binary_subscr() |
paul@7 | 675 | |
paul@7 | 676 | def aastore(self, arguments, program): |
paul@7 | 677 | # NOTE: No type checking performed. |
paul@7 | 678 | # Stack: arrayref, index, value |
paul@7 | 679 | program.rot_three() # Stack: value, arrayref, index |
paul@7 | 680 | program.store_subscr() |
paul@7 | 681 | |
paul@7 | 682 | def aconst_null(self, arguments, program): |
paul@7 | 683 | program.load_global(None) |
paul@7 | 684 | |
paul@7 | 685 | def aload(self, arguments, program): |
paul@7 | 686 | program.load_fast(arguments[0]) |
paul@7 | 687 | |
paul@7 | 688 | def aload_0(self, arguments, program): |
paul@7 | 689 | program.load_fast(0) |
paul@7 | 690 | |
paul@7 | 691 | def aload_1(self, arguments, program): |
paul@7 | 692 | program.load_fast(1) |
paul@7 | 693 | |
paul@7 | 694 | def aload_2(self, arguments, program): |
paul@7 | 695 | program.load_fast(2) |
paul@7 | 696 | |
paul@7 | 697 | def aload_3(self, arguments, program): |
paul@7 | 698 | program.load_fast(3) |
paul@7 | 699 | |
paul@7 | 700 | def anewarray(self, arguments, program): |
paul@7 | 701 | # NOTE: Does not raise NegativeArraySizeException. |
paul@7 | 702 | # NOTE: Not using the index to type the list/array. |
paul@7 | 703 | index = (arguments[0] << 8) + arguments[1] |
paul@8 | 704 | self._newarray(program) |
paul@7 | 705 | |
paul@8 | 706 | def _newarray(self, program): |
paul@7 | 707 | program.build_list() # Stack: count, list |
paul@7 | 708 | program.rot_two() # Stack: list, count |
paul@7 | 709 | program.setup_loop() |
paul@7 | 710 | program.load_global("range") |
paul@7 | 711 | program.load_const(0) # Stack: list, count, range, 0 |
paul@7 | 712 | program.rot_three() # Stack: list, 0, count, range |
paul@7 | 713 | program.rot_three() # Stack: list, range, 0, count |
paul@7 | 714 | program.call_function(2) # Stack: list, range_list |
paul@7 | 715 | program.get_iter() # Stack: list, iter |
paul@7 | 716 | program.for_iter() # Stack: list, iter, value |
paul@7 | 717 | program.pop_top() # Stack: list, iter |
paul@7 | 718 | program.rot_two() # Stack: iter, list |
paul@7 | 719 | program.dup_top() # Stack: iter, list, list |
paul@7 | 720 | program.load_attr("append") # Stack: iter, list, append |
paul@7 | 721 | program.load_global(None) # Stack: iter, list, append, None |
paul@7 | 722 | program.call_function(1) # Stack: iter, list, None |
paul@7 | 723 | program.pop_top() # Stack: iter, list |
paul@7 | 724 | program.rot_two() # Stack: list, iter |
paul@7 | 725 | program.end_loop() # Back to for_iter above |
paul@7 | 726 | |
paul@7 | 727 | def areturn(self, arguments, program): |
paul@7 | 728 | program.return_value() |
paul@7 | 729 | |
paul@7 | 730 | def arraylength(self, arguments, program): |
paul@7 | 731 | program.load_global("len") # Stack: arrayref, len |
paul@7 | 732 | program.rot_two() # Stack: len, arrayref |
paul@7 | 733 | program.call_function(1) |
paul@7 | 734 | |
paul@7 | 735 | def astore(self, arguments, program): |
paul@7 | 736 | program.store_fast(arguments[0]) |
paul@7 | 737 | |
paul@7 | 738 | def astore_0(self, arguments, program): |
paul@7 | 739 | program.store_fast(0) |
paul@7 | 740 | |
paul@7 | 741 | def astore_1(self, arguments, program): |
paul@7 | 742 | program.store_fast(1) |
paul@7 | 743 | |
paul@7 | 744 | def astore_2(self, arguments, program): |
paul@7 | 745 | program.store_fast(2) |
paul@7 | 746 | |
paul@7 | 747 | def astore_3(self, arguments, program): |
paul@7 | 748 | program.store_fast(3) |
paul@7 | 749 | |
paul@7 | 750 | def athrow(self, arguments, program): |
paul@7 | 751 | # NOTE: NullPointerException not raised where null/None is found on the stack. |
paul@7 | 752 | program.raise_varargs(1) |
paul@7 | 753 | |
paul@7 | 754 | baload = aaload |
paul@7 | 755 | bastore = aastore |
paul@7 | 756 | |
paul@7 | 757 | def bipush(self, arguments, program): |
paul@7 | 758 | program.load_const(arguments[0]) |
paul@7 | 759 | |
paul@7 | 760 | caload = aaload |
paul@7 | 761 | castore = aastore |
paul@7 | 762 | |
paul@7 | 763 | def checkcast(self, arguments, program): |
paul@7 | 764 | index = (arguments[0] << 8) + arguments[1] |
paul@7 | 765 | target_name = self.class_file.constants[index - 1].get_name() |
paul@7 | 766 | target_components = target_name.split("/") |
paul@7 | 767 | |
paul@7 | 768 | program.dup_top() # Stack: objectref, objectref |
paul@7 | 769 | program.load_global("isinstance") # Stack: objectref, objectref, isinstance |
paul@7 | 770 | program.rot_two() # Stack: objectref, isinstance, objectref |
paul@7 | 771 | program.load_global(target_components[0]) |
paul@7 | 772 | for target_component in target_components[1:]: |
paul@7 | 773 | program.load_attr(target_component) |
paul@7 | 774 | program.call_function(2) # Stack: objectref |
paul@7 | 775 | |
paul@7 | 776 | def d2f(self, arguments, program): |
paul@7 | 777 | pass |
paul@7 | 778 | |
paul@7 | 779 | def d2i(self, arguments, program): |
paul@7 | 780 | program.load_global("int") # Stack: value, int |
paul@7 | 781 | program.rot_two() # Stack: int, value |
paul@7 | 782 | program.call_function(1) # Stack: result |
paul@7 | 783 | |
paul@7 | 784 | d2l = d2i # Preserving Java semantics |
paul@7 | 785 | |
paul@7 | 786 | def dadd(self, arguments, program): |
paul@7 | 787 | # NOTE: No type checking performed. |
paul@7 | 788 | program.binary_add() |
paul@7 | 789 | |
paul@7 | 790 | daload = aaload |
paul@7 | 791 | dastore = aastore |
paul@7 | 792 | |
paul@7 | 793 | def dcmpg(self, arguments, program): |
paul@7 | 794 | # NOTE: No type checking performed. |
paul@7 | 795 | program.compare_op(">") |
paul@7 | 796 | |
paul@7 | 797 | def dcmpl(self, arguments, program): |
paul@7 | 798 | # NOTE: No type checking performed. |
paul@7 | 799 | program.compare_op("<") |
paul@7 | 800 | |
paul@7 | 801 | def dconst_0(self, arguments, program): |
paul@7 | 802 | program.load_const(0.0) |
paul@7 | 803 | |
paul@7 | 804 | def dconst_1(self, arguments, program): |
paul@7 | 805 | program.load_const(1.0) |
paul@7 | 806 | |
paul@7 | 807 | def ddiv(self, arguments, program): |
paul@7 | 808 | # NOTE: No type checking performed. |
paul@7 | 809 | program.binary_divide() |
paul@7 | 810 | |
paul@7 | 811 | dload = aload |
paul@7 | 812 | dload_0 = aload_0 |
paul@7 | 813 | dload_1 = aload_1 |
paul@7 | 814 | dload_2 = aload_2 |
paul@7 | 815 | dload_3 = aload_3 |
paul@7 | 816 | |
paul@7 | 817 | def dmul(self, arguments, program): |
paul@7 | 818 | # NOTE: No type checking performed. |
paul@7 | 819 | program.binary_multiply() |
paul@7 | 820 | |
paul@7 | 821 | def dneg(self, arguments, program): |
paul@7 | 822 | # NOTE: No type checking performed. |
paul@7 | 823 | program.unary_negative() |
paul@7 | 824 | |
paul@7 | 825 | def drem(self, arguments, program): |
paul@7 | 826 | # NOTE: No type checking performed. |
paul@7 | 827 | program.binary_modulo() |
paul@7 | 828 | |
paul@7 | 829 | dreturn = areturn |
paul@7 | 830 | dstore = astore |
paul@7 | 831 | dstore_0 = astore_0 |
paul@7 | 832 | dstore_1 = astore_1 |
paul@7 | 833 | dstore_2 = astore_2 |
paul@7 | 834 | dstore_3 = astore_3 |
paul@7 | 835 | |
paul@7 | 836 | def dsub(self, arguments, program): |
paul@7 | 837 | # NOTE: No type checking performed. |
paul@7 | 838 | program.binary_subtract() |
paul@7 | 839 | |
paul@7 | 840 | def dup(self, arguments, program): |
paul@7 | 841 | program.dup_top() |
paul@7 | 842 | |
paul@7 | 843 | def dup_x1(self, arguments, program): |
paul@7 | 844 | # Ignoring computational type categories. |
paul@7 | 845 | program.dup_top() |
paul@7 | 846 | program.rot_three() |
paul@7 | 847 | |
paul@7 | 848 | def dup_x2(self, arguments, program): |
paul@7 | 849 | # Ignoring computational type categories. |
paul@7 | 850 | program.dup_top() |
paul@7 | 851 | program.rot_four() |
paul@7 | 852 | |
paul@7 | 853 | dup2 = dup # Ignoring computational type categories |
paul@7 | 854 | dup2_x1 = dup_x1 # Ignoring computational type categories |
paul@7 | 855 | dup2_x2 = dup_x2 # Ignoring computational type categories |
paul@7 | 856 | |
paul@7 | 857 | def f2d(self, arguments, program): |
paul@7 | 858 | pass # Preserving Java semantics |
paul@7 | 859 | |
paul@7 | 860 | def f2i(self, arguments, program): |
paul@7 | 861 | program.load_global("int") # Stack: value, int |
paul@7 | 862 | program.rot_two() # Stack: int, value |
paul@7 | 863 | program.call_function(1) # Stack: result |
paul@7 | 864 | |
paul@7 | 865 | f2l = f2i # Preserving Java semantics |
paul@7 | 866 | fadd = dadd |
paul@7 | 867 | faload = daload |
paul@7 | 868 | fastore = dastore |
paul@7 | 869 | fcmpg = dcmpg |
paul@7 | 870 | fcmpl = dcmpl |
paul@7 | 871 | fconst_0 = dconst_0 |
paul@7 | 872 | fconst_1 = dconst_1 |
paul@7 | 873 | |
paul@7 | 874 | def fconst_2(self, arguments, program): |
paul@7 | 875 | program.load_const(2.0) |
paul@7 | 876 | |
paul@7 | 877 | fdiv = ddiv |
paul@7 | 878 | fload = dload |
paul@7 | 879 | fload_0 = dload_0 |
paul@7 | 880 | fload_1 = dload_1 |
paul@7 | 881 | fload_2 = dload_2 |
paul@7 | 882 | fload_3 = dload_3 |
paul@7 | 883 | fmul = dmul |
paul@7 | 884 | fneg = dneg |
paul@7 | 885 | frem = drem |
paul@7 | 886 | freturn = dreturn |
paul@7 | 887 | fstore = dstore |
paul@7 | 888 | fstore_0 = dstore_0 |
paul@7 | 889 | fstore_1 = dstore_1 |
paul@7 | 890 | fstore_2 = dstore_2 |
paul@7 | 891 | fstore_3 = dstore_3 |
paul@7 | 892 | fsub = dsub |
paul@7 | 893 | |
paul@7 | 894 | def getfield(self, arguments, program): |
paul@7 | 895 | index = (arguments[0] << 8) + arguments[1] |
paul@7 | 896 | target_name = self.class_file.constants[index - 1].get_name() |
paul@7 | 897 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@7 | 898 | program.load_attr(str(target_name)) |
paul@7 | 899 | |
paul@8 | 900 | def getstatic(self, arguments, program): |
paul@8 | 901 | index = (arguments[0] << 8) + arguments[1] |
paul@8 | 902 | target_name = self.class_file.constants[index - 1].get_name() |
paul@8 | 903 | program.load_name("self") |
paul@8 | 904 | program.load_attr("__class__") |
paul@8 | 905 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@8 | 906 | program.load_attr(str(target_name)) |
paul@7 | 907 | |
paul@7 | 908 | def goto(self, arguments, program): |
paul@7 | 909 | offset = signed2((arguments[0] << 8) + arguments[1]) |
paul@7 | 910 | java_absolute = self.java_position + offset |
paul@7 | 911 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@7 | 912 | |
paul@7 | 913 | def goto_w(self, arguments, program): |
paul@7 | 914 | offset = signed4((arguments[0] << 24) + (arguments[1] << 16) + (arguments[2] << 8) + arguments[3]) |
paul@7 | 915 | java_absolute = self.java_position + offset |
paul@7 | 916 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@7 | 917 | |
paul@7 | 918 | def i2b(self, arguments, program): |
paul@7 | 919 | pass |
paul@7 | 920 | |
paul@7 | 921 | def i2c(self, arguments, program): |
paul@7 | 922 | program.load_global("chr") # Stack: value, chr |
paul@7 | 923 | program.rot_two() # Stack: chr, value |
paul@7 | 924 | program.call_function(1) # Stack: result |
paul@7 | 925 | |
paul@7 | 926 | def i2d(self, arguments, program): |
paul@7 | 927 | program.load_global("float") # Stack: value, float |
paul@7 | 928 | program.rot_two() # Stack: float, value |
paul@7 | 929 | program.call_function(1) # Stack: result |
paul@7 | 930 | |
paul@7 | 931 | i2f = i2d # Not distinguishing between float and double |
paul@7 | 932 | |
paul@7 | 933 | def i2l(self, arguments, program): |
paul@7 | 934 | pass # Preserving Java semantics |
paul@7 | 935 | |
paul@7 | 936 | def i2s(self, arguments, program): |
paul@7 | 937 | pass # Not distinguishing between int and short |
paul@7 | 938 | |
paul@7 | 939 | iadd = fadd |
paul@7 | 940 | iaload = faload |
paul@7 | 941 | |
paul@7 | 942 | def iand(self, arguments, program): |
paul@7 | 943 | # NOTE: No type checking performed. |
paul@7 | 944 | program.binary_and() |
paul@7 | 945 | |
paul@7 | 946 | iastore = fastore |
paul@7 | 947 | |
paul@7 | 948 | def iconst_m1(self, arguments, program): |
paul@7 | 949 | program.load_const(-1) |
paul@7 | 950 | |
paul@7 | 951 | def iconst_0(self, arguments, program): |
paul@7 | 952 | program.load_const(0) |
paul@7 | 953 | |
paul@7 | 954 | def iconst_1(self, arguments, program): |
paul@7 | 955 | program.load_const(1) |
paul@7 | 956 | |
paul@7 | 957 | def iconst_2(self, arguments, program): |
paul@7 | 958 | program.load_const(2) |
paul@7 | 959 | |
paul@7 | 960 | def iconst_3(self, arguments, program): |
paul@7 | 961 | program.load_const(3) |
paul@7 | 962 | |
paul@7 | 963 | def iconst_4(self, arguments, program): |
paul@7 | 964 | program.load_const(4) |
paul@7 | 965 | |
paul@7 | 966 | def iconst_5(self, arguments, program): |
paul@7 | 967 | program.load_const(5) |
paul@7 | 968 | |
paul@7 | 969 | idiv = fdiv |
paul@7 | 970 | |
paul@7 | 971 | def _if_xcmpx(self, arguments, program, op): |
paul@7 | 972 | offset = signed2((arguments[0] << 8) + arguments[1]) |
paul@7 | 973 | java_absolute = self.java_position + offset |
paul@7 | 974 | program.compare_op(op) |
paul@7 | 975 | program.jump_to_label(0, "next") # skip if false |
paul@7 | 976 | program.goto(offset) |
paul@7 | 977 | program.start_label("next") |
paul@7 | 978 | |
paul@7 | 979 | def if_acmpeq(self, arguments, program): |
paul@7 | 980 | # NOTE: No type checking performed. |
paul@7 | 981 | self._if_xcmpx(arguments, program, "is") |
paul@7 | 982 | |
paul@7 | 983 | def if_acmpne(self, arguments, program): |
paul@7 | 984 | # NOTE: No type checking performed. |
paul@7 | 985 | self._if_xcmpx(arguments, program, "is not") |
paul@7 | 986 | |
paul@7 | 987 | def if_icmpeq(self, arguments, program): |
paul@7 | 988 | # NOTE: No type checking performed. |
paul@7 | 989 | self._if_xcmpx(arguments, program, "==") |
paul@7 | 990 | |
paul@7 | 991 | def if_icmpne(self, arguments, program): |
paul@7 | 992 | # NOTE: No type checking performed. |
paul@7 | 993 | self._if_xcmpx(arguments, program, "!=") |
paul@7 | 994 | |
paul@7 | 995 | def if_icmplt(self, arguments, program): |
paul@7 | 996 | # NOTE: No type checking performed. |
paul@7 | 997 | self._if_xcmpx(arguments, program, "<") |
paul@7 | 998 | |
paul@7 | 999 | def if_icmpge(self, arguments, program): |
paul@7 | 1000 | # NOTE: No type checking performed. |
paul@7 | 1001 | self._if_xcmpx(arguments, program, ">=") |
paul@7 | 1002 | |
paul@7 | 1003 | def if_icmpgt(self, arguments, program): |
paul@7 | 1004 | # NOTE: No type checking performed. |
paul@7 | 1005 | self._if_xcmpx(arguments, program, ">") |
paul@7 | 1006 | |
paul@7 | 1007 | def if_icmple(self, arguments, program): |
paul@7 | 1008 | # NOTE: No type checking performed. |
paul@7 | 1009 | self._if_xcmpx(arguments, program, "<=") |
paul@7 | 1010 | |
paul@7 | 1011 | def ifeq(self, arguments, program): |
paul@7 | 1012 | # NOTE: No type checking performed. |
paul@7 | 1013 | program.load_const(0) |
paul@7 | 1014 | self._if_xcmpx(arguments, program, "==") |
paul@7 | 1015 | |
paul@7 | 1016 | def ifne(self, arguments, program): |
paul@7 | 1017 | # NOTE: No type checking performed. |
paul@7 | 1018 | program.load_const(0) |
paul@7 | 1019 | self._if_xcmpx(arguments, program, "!=") |
paul@7 | 1020 | |
paul@7 | 1021 | def iflt(self, arguments, program): |
paul@7 | 1022 | # NOTE: No type checking performed. |
paul@7 | 1023 | program.load_const(0) |
paul@7 | 1024 | self._if_xcmpx(arguments, program, "<") |
paul@7 | 1025 | |
paul@7 | 1026 | def ifge(self, arguments, program): |
paul@7 | 1027 | # NOTE: No type checking performed. |
paul@7 | 1028 | program.load_const(0) |
paul@7 | 1029 | self._if_xcmpx(arguments, program, ">=") |
paul@7 | 1030 | |
paul@7 | 1031 | def ifgt(self, arguments, program): |
paul@7 | 1032 | # NOTE: No type checking performed. |
paul@7 | 1033 | program.load_const(0) |
paul@7 | 1034 | self._if_xcmpx(arguments, program, ">") |
paul@7 | 1035 | |
paul@7 | 1036 | def ifle(self, arguments, program): |
paul@7 | 1037 | # NOTE: No type checking performed. |
paul@7 | 1038 | program.load_const(0) |
paul@7 | 1039 | self._if_xcmpx(arguments, program, "<=") |
paul@7 | 1040 | |
paul@7 | 1041 | def ifnonnull(self, arguments, program): |
paul@7 | 1042 | # NOTE: No type checking performed. |
paul@7 | 1043 | program.load_const(None) |
paul@7 | 1044 | self._if_xcmpx(arguments, program, "is not") |
paul@7 | 1045 | |
paul@7 | 1046 | def ifnull(self, arguments, program): |
paul@7 | 1047 | # NOTE: No type checking performed. |
paul@7 | 1048 | program.load_const(None) |
paul@7 | 1049 | self._if_xcmpx(arguments, program, "is") |
paul@7 | 1050 | |
paul@7 | 1051 | def iinc(self, arguments, program): |
paul@7 | 1052 | # NOTE: No type checking performed. |
paul@7 | 1053 | program.load_fast(arguments[0]) |
paul@7 | 1054 | program.load_const(arguments[1]) |
paul@7 | 1055 | program.binary_add() |
paul@7 | 1056 | |
paul@7 | 1057 | iload = fload |
paul@7 | 1058 | iload_0 = fload_0 |
paul@7 | 1059 | iload_1 = fload_1 |
paul@7 | 1060 | iload_2 = fload_2 |
paul@7 | 1061 | iload_3 = fload_3 |
paul@7 | 1062 | imul = fmul |
paul@7 | 1063 | ineg = fneg |
paul@7 | 1064 | |
paul@7 | 1065 | def instanceof(self, arguments, program): |
paul@7 | 1066 | index = (arguments[0] << 8) + arguments[1] |
paul@7 | 1067 | target_name = self.class_file.constants[index - 1].get_name() |
paul@7 | 1068 | target_components = target_name.split("/") |
paul@7 | 1069 | |
paul@7 | 1070 | program.load_global("isinstance") # Stack: objectref, isinstance |
paul@7 | 1071 | program.rot_two() # Stack: isinstance, objectref |
paul@7 | 1072 | program.load_global(target_components[0]) |
paul@7 | 1073 | for target_component in target_components[1:]: |
paul@7 | 1074 | program.load_attr(target_component) |
paul@7 | 1075 | program.call_function(2) # Stack: result |
paul@7 | 1076 | |
paul@7 | 1077 | def _invoke(self, target_name, program): |
paul@7 | 1078 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@7 | 1079 | program.load_attr(str(target_name)) # Stack: tuple, method |
paul@7 | 1080 | program.rot_two() # Stack: method, tuple |
paul@7 | 1081 | program.load_global("apply") # Stack: method, tuple, apply |
paul@7 | 1082 | program.rot_three() # Stack: apply, method, tuple |
paul@7 | 1083 | program.call_function(2) |
paul@7 | 1084 | |
paul@7 | 1085 | def invokeinterface(self, arguments, program): |
paul@7 | 1086 | # NOTE: This implementation does not perform the necessary checks for |
paul@7 | 1087 | # NOTE: signature-based polymorphism. |
paul@7 | 1088 | # NOTE: Java rules not specifically obeyed. |
paul@7 | 1089 | index = (arguments[0] << 8) + arguments[1] |
paul@7 | 1090 | count = arguments[2] |
paul@7 | 1091 | target_name = self.class_file.constants[index - 1].get_name() |
paul@7 | 1092 | # Stack: objectref, arg1, arg2, ... |
paul@7 | 1093 | program.build_tuple(count) # Stack: objectref, tuple |
paul@8 | 1094 | program.rot_two() # Stack: tuple, objectref |
paul@7 | 1095 | self._invoke(target_name, program) |
paul@7 | 1096 | |
paul@7 | 1097 | def invokespecial(self, arguments, program): |
paul@7 | 1098 | # NOTE: This implementation does not perform the necessary checks for |
paul@7 | 1099 | # NOTE: signature-based polymorphism. |
paul@7 | 1100 | # NOTE: Java rules not specifically obeyed. |
paul@7 | 1101 | index = (arguments[0] << 8) + arguments[1] |
paul@7 | 1102 | target = self.class_file.constants[index - 1] |
paul@7 | 1103 | target_name = target.get_name() |
paul@7 | 1104 | # Get the number of parameters from the descriptor. |
paul@7 | 1105 | count = len(target.get_descriptor()[0]) |
paul@7 | 1106 | # Stack: objectref, arg1, arg2, ... |
paul@8 | 1107 | program.build_tuple(count + 1) # Stack: tuple |
paul@8 | 1108 | # Use the class to provide access to static methods. |
paul@8 | 1109 | program.load_name("self") # Stack: tuple, self |
paul@8 | 1110 | program.load_attr("__class__") # Stack: tuple, class |
paul@8 | 1111 | program.load_attr("__bases__") # Stack: tuple, base-classes |
paul@8 | 1112 | program.dup_top() # Stack: tuple, base-classes, base-classes |
paul@8 | 1113 | program.load_global("len") # Stack: tuple, base-classes, base-classes, len |
paul@8 | 1114 | program.rot_two() # Stack: tuple, base-classes, len, base-classes |
paul@8 | 1115 | program.call_function(1) # Stack: tuple, base-classes, count |
paul@8 | 1116 | program.load_const(0) # Stack: tuple, base-classes, count, 0 |
paul@8 | 1117 | program.compare_op("==") # Stack: tuple, base-classes, result |
paul@8 | 1118 | program.jump_to_label(1, "next") |
paul@8 | 1119 | program.pop_top() # Stack: tuple, base-classes |
paul@8 | 1120 | program.load_const(0) # Stack: tuple, base-classes, 0 |
paul@8 | 1121 | program.binary_subscr() # Stack: tuple, superclass |
paul@7 | 1122 | self._invoke(target_name, program) |
paul@8 | 1123 | program.jump_to_label(None, "next2") |
paul@8 | 1124 | program.start_label("next") |
paul@8 | 1125 | program.pop_top() # Stack: tuple, base-classes |
paul@8 | 1126 | program.pop_top() # Stack: tuple |
paul@8 | 1127 | program.pop_top() # Stack: |
paul@8 | 1128 | program.start_label("next2") |
paul@7 | 1129 | |
paul@7 | 1130 | def invokestatic(self, arguments, program): |
paul@7 | 1131 | # NOTE: This implementation does not perform the necessary checks for |
paul@7 | 1132 | # NOTE: signature-based polymorphism. |
paul@7 | 1133 | # NOTE: Java rules not specifically obeyed. |
paul@7 | 1134 | index = (arguments[0] << 8) + arguments[1] |
paul@7 | 1135 | target = self.class_file.constants[index - 1] |
paul@7 | 1136 | target_name = target.get_name() |
paul@7 | 1137 | # Get the number of parameters from the descriptor. |
paul@7 | 1138 | count = len(target.get_descriptor()[0]) |
paul@7 | 1139 | # Stack: arg1, arg2, ... |
paul@8 | 1140 | program.build_tuple(count) # Stack: tuple |
paul@8 | 1141 | # Use the class to provide access to static methods. |
paul@8 | 1142 | program.load_name("self") # Stack: tuple, self |
paul@8 | 1143 | program.load_attr("__class__") # Stack: tuple, class |
paul@7 | 1144 | self._invoke(target_name, program) |
paul@7 | 1145 | |
paul@7 | 1146 | invokevirtual = invokeinterface # Ignoring Java rules |
paul@7 | 1147 | |
paul@7 | 1148 | def ior(self, arguments, program): |
paul@7 | 1149 | # NOTE: No type checking performed. |
paul@7 | 1150 | program.binary_or() |
paul@7 | 1151 | |
paul@7 | 1152 | irem = frem |
paul@7 | 1153 | ireturn = freturn |
paul@7 | 1154 | |
paul@7 | 1155 | def ishl(self, arguments, program): |
paul@7 | 1156 | # NOTE: No type checking performed. |
paul@7 | 1157 | # NOTE: Not verified. |
paul@7 | 1158 | program.binary_lshift() |
paul@7 | 1159 | |
paul@7 | 1160 | def ishr(self, arguments, program): |
paul@7 | 1161 | # NOTE: No type checking performed. |
paul@7 | 1162 | # NOTE: Not verified. |
paul@7 | 1163 | program.binary_rshift() |
paul@7 | 1164 | |
paul@7 | 1165 | istore = fstore |
paul@7 | 1166 | istore_0 = fstore_0 |
paul@7 | 1167 | istore_1 = fstore_1 |
paul@7 | 1168 | istore_2 = fstore_2 |
paul@7 | 1169 | istore_3 = fstore_3 |
paul@7 | 1170 | isub = fsub |
paul@7 | 1171 | iushr = ishr # Ignoring distinctions between arithmetic and logical shifts |
paul@7 | 1172 | |
paul@7 | 1173 | def ixor(self, arguments, program): |
paul@7 | 1174 | # NOTE: No type checking performed. |
paul@7 | 1175 | program.binary_xor() |
paul@7 | 1176 | |
paul@7 | 1177 | def jsr(self, arguments, program): |
paul@7 | 1178 | offset = signed2((arguments[0] << 8) + arguments[1]) |
paul@7 | 1179 | java_absolute = self.java_position + offset |
paul@7 | 1180 | # Store the address of the next instruction. |
paul@8 | 1181 | program.load_const_ret(self.position_mapping[self.java_position + 3]) |
paul@7 | 1182 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@7 | 1183 | |
paul@7 | 1184 | def jsr_w(self, arguments, program): |
paul@7 | 1185 | offset = signed4((arguments[0] << 24) + (arguments[1] << 16) + (arguments[2] << 8) + arguments[3]) |
paul@7 | 1186 | java_absolute = self.java_position + offset |
paul@7 | 1187 | # Store the address of the next instruction. |
paul@8 | 1188 | program.load_const_ret(self.position_mapping[self.java_position + 5]) |
paul@7 | 1189 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@7 | 1190 | |
paul@7 | 1191 | l2d = i2d |
paul@7 | 1192 | l2f = i2f |
paul@7 | 1193 | |
paul@7 | 1194 | def l2i(self, arguments, program): |
paul@7 | 1195 | pass # Preserving Java semantics |
paul@7 | 1196 | |
paul@7 | 1197 | ladd = iadd |
paul@7 | 1198 | laload = iaload |
paul@7 | 1199 | land = iand |
paul@7 | 1200 | lastore = iastore |
paul@7 | 1201 | |
paul@7 | 1202 | def lcmp(self, arguments, program): |
paul@7 | 1203 | # NOTE: No type checking performed. |
paul@7 | 1204 | program.dup_topx(2) # Stack: value1, value2, value1, value2 |
paul@7 | 1205 | program.compare_op(">") # Stack: value1, value2, result |
paul@7 | 1206 | program.jump_to_label(0, "equals") |
paul@7 | 1207 | # True - produce result and branch. |
paul@7 | 1208 | program.pop_top() # Stack: value1, value2 |
paul@7 | 1209 | program.pop_top() # Stack: value1 |
paul@7 | 1210 | program.pop_top() # Stack: |
paul@7 | 1211 | program.load_const(1) # Stack: 1 |
paul@7 | 1212 | program.jump_to_label(None, "next") |
paul@7 | 1213 | # False - test equality. |
paul@7 | 1214 | program.start_label("equals") |
paul@7 | 1215 | program.pop_top() # Stack: value1, value2 |
paul@7 | 1216 | program.dup_topx(2) # Stack: value1, value2, value1, value2 |
paul@7 | 1217 | program.compare_op("==") # Stack: value1, value2, result |
paul@7 | 1218 | program.jump_to_label(0, "less") |
paul@7 | 1219 | # True - produce result and branch. |
paul@7 | 1220 | program.pop_top() # Stack: value1, value2 |
paul@7 | 1221 | program.pop_top() # Stack: value1 |
paul@7 | 1222 | program.pop_top() # Stack: |
paul@7 | 1223 | program.load_const(0) # Stack: 0 |
paul@7 | 1224 | program.jump_to_label(None, "next") |
paul@7 | 1225 | # False - produce result. |
paul@7 | 1226 | program.start_label("less") |
paul@7 | 1227 | program.pop_top() # Stack: value1, value2 |
paul@7 | 1228 | program.pop_top() # Stack: value1 |
paul@7 | 1229 | program.pop_top() # Stack: |
paul@7 | 1230 | program.load_const(-1) # Stack: -1 |
paul@7 | 1231 | program.start_label("next") |
paul@7 | 1232 | |
paul@7 | 1233 | lconst_0 = iconst_0 |
paul@7 | 1234 | lconst_1 = iconst_1 |
paul@7 | 1235 | |
paul@7 | 1236 | def ldc(self, arguments, program): |
paul@7 | 1237 | program.load_const(self.class_file.constants[arguments[0] - 1]) |
paul@7 | 1238 | |
paul@7 | 1239 | def ldc_w(self, arguments, program): |
paul@7 | 1240 | program.load_const(self.class_file.constants[(arguments[0] << 8) + arguments[1] - 1]) |
paul@7 | 1241 | |
paul@7 | 1242 | ldc2_w = ldc_w |
paul@7 | 1243 | ldiv = idiv |
paul@7 | 1244 | lload = iload |
paul@7 | 1245 | lload_0 = iload_0 |
paul@7 | 1246 | lload_1 = iload_1 |
paul@7 | 1247 | lload_2 = iload_2 |
paul@7 | 1248 | lload_3 = iload_3 |
paul@7 | 1249 | lmul = imul |
paul@7 | 1250 | lneg = ineg |
paul@7 | 1251 | |
paul@7 | 1252 | def lookupswitch(self, arguments, program): |
paul@7 | 1253 | # Find the offset to the next 4 byte boundary in the code. |
paul@7 | 1254 | d, r = divmod(self.java_position, 4) |
paul@7 | 1255 | to_boundary = (4 - r) % 4 |
paul@7 | 1256 | # Get the pertinent arguments. |
paul@7 | 1257 | arguments = arguments[to_boundary:] |
paul@7 | 1258 | default = (arguments[0] << 24) + (arguments[1] << 16) + (arguments[2] << 8) + arguments[3] |
paul@7 | 1259 | npairs = (arguments[4] << 24) + (arguments[5] << 16) + (arguments[6] << 8) + arguments[7] |
paul@7 | 1260 | # Process the pairs. |
paul@7 | 1261 | # NOTE: This is not the most optimal implementation. |
paul@7 | 1262 | pair_index = 8 |
paul@7 | 1263 | for pair in range(0, npairs): |
paul@7 | 1264 | match = ((arguments[pair_index] << 24) + (arguments[pair_index + 1] << 16) + |
paul@7 | 1265 | (arguments[pair_index + 2] << 8) + arguments[pair_index + 3]) |
paul@7 | 1266 | offset = signed4((arguments[pair_index + 4] << 24) + (arguments[pair_index + 5] << 16) + |
paul@7 | 1267 | (arguments[pair_index + 6] << 8) + arguments[pair_index + 7]) |
paul@7 | 1268 | # Calculate the branch target. |
paul@7 | 1269 | java_absolute = self.java_position + offset |
paul@7 | 1270 | # Generate branching code. |
paul@7 | 1271 | program.dup_top() # Stack: key, key |
paul@7 | 1272 | program.load_const(match) # Stack: key, key, match |
paul@7 | 1273 | program.compare_op("==") # Stack: key, result |
paul@8 | 1274 | program.jump_to_label(0, "end") |
paul@7 | 1275 | program.pop_top() # Stack: key |
paul@7 | 1276 | program.pop_top() # Stack: |
paul@7 | 1277 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@7 | 1278 | # Generate the label for the end of the branching code. |
paul@8 | 1279 | program.start_label("end") |
paul@7 | 1280 | program.pop_top() # Stack: key |
paul@7 | 1281 | # Update the index. |
paul@7 | 1282 | pair_index += 8 |
paul@7 | 1283 | # Generate the default. |
paul@7 | 1284 | java_absolute = self.java_position + default |
paul@7 | 1285 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@7 | 1286 | |
paul@7 | 1287 | lor = ior |
paul@7 | 1288 | lrem = irem |
paul@7 | 1289 | lreturn = ireturn |
paul@7 | 1290 | lshl = ishl |
paul@7 | 1291 | lshr = ishr |
paul@7 | 1292 | lstore = istore |
paul@7 | 1293 | lstore_0 = istore_0 |
paul@7 | 1294 | lstore_1 = istore_1 |
paul@7 | 1295 | lstore_2 = istore_2 |
paul@7 | 1296 | lstore_3 = istore_3 |
paul@7 | 1297 | lsub = isub |
paul@7 | 1298 | lushr = iushr |
paul@7 | 1299 | lxor = ixor |
paul@7 | 1300 | |
paul@7 | 1301 | def monitorenter(self, arguments, program): |
paul@7 | 1302 | # NOTE: To be implemented. |
paul@7 | 1303 | pass |
paul@7 | 1304 | |
paul@7 | 1305 | def monitorexit(self, arguments, program): |
paul@7 | 1306 | # NOTE: To be implemented. |
paul@7 | 1307 | pass |
paul@7 | 1308 | |
paul@7 | 1309 | def multianewarray(self, arguments, program): |
paul@8 | 1310 | # NOTE: To be implemented. |
paul@8 | 1311 | pass |
paul@8 | 1312 | |
paul@8 | 1313 | def new(self, arguments, program): |
paul@8 | 1314 | # This operation is considered to be the same as the calling of the |
paul@8 | 1315 | # initialisation method of the given class with no arguments. |
paul@8 | 1316 | index = (arguments[0] << 8) + arguments[1] |
paul@8 | 1317 | target_name = self.class_file.constants[index - 1].get_name() |
paul@8 | 1318 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@8 | 1319 | program.load_global(str(target_name)) |
paul@8 | 1320 | program.call_function(0) |
paul@8 | 1321 | |
paul@8 | 1322 | def newarray(self, arguments, program): |
paul@8 | 1323 | # NOTE: Does not raise NegativeArraySizeException. |
paul@8 | 1324 | # NOTE: Not using the arguments to type the list/array. |
paul@8 | 1325 | self._newarray(program) |
paul@8 | 1326 | |
paul@8 | 1327 | def nop(self, arguments, program): |
paul@8 | 1328 | pass |
paul@8 | 1329 | |
paul@8 | 1330 | def pop(self, arguments, program): |
paul@8 | 1331 | program.pop_top() |
paul@8 | 1332 | |
paul@8 | 1333 | pop2 = pop # ignoring Java stack value distinctions |
paul@8 | 1334 | |
paul@8 | 1335 | def putfield(self, arguments, program): |
paul@8 | 1336 | index = (arguments[0] << 8) + arguments[1] |
paul@8 | 1337 | target_name = self.class_file.constants[index - 1].get_name() |
paul@8 | 1338 | program.rot_two() |
paul@8 | 1339 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@8 | 1340 | program.store_attr(str(target_name)) |
paul@8 | 1341 | |
paul@8 | 1342 | def putstatic(self, arguments, program): |
paul@8 | 1343 | index = (arguments[0] << 8) + arguments[1] |
paul@8 | 1344 | target_name = self.class_file.constants[index - 1].get_name() |
paul@8 | 1345 | program.load_name("self") |
paul@8 | 1346 | program.load_attr("__class__") |
paul@8 | 1347 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@8 | 1348 | program.store_attr(str(target_name)) |
paul@8 | 1349 | |
paul@8 | 1350 | def ret(self, arguments, program): |
paul@8 | 1351 | program.ret(arguments[0]) |
paul@8 | 1352 | |
paul@8 | 1353 | def return_(self, arguments, program): |
paul@8 | 1354 | program.load_const(None) |
paul@8 | 1355 | program.return_value() |
paul@8 | 1356 | |
paul@8 | 1357 | saload = laload |
paul@8 | 1358 | sastore = lastore |
paul@8 | 1359 | |
paul@8 | 1360 | def sipush(self, arguments, program): |
paul@8 | 1361 | program.load_const((arguments[0] << 8) + arguments[1]) |
paul@8 | 1362 | |
paul@8 | 1363 | def swap(self, arguments, program): |
paul@8 | 1364 | program.rot_two() |
paul@8 | 1365 | |
paul@8 | 1366 | def tableswitch(self, arguments, program): |
paul@8 | 1367 | # Find the offset to the next 4 byte boundary in the code. |
paul@8 | 1368 | d, r = divmod(self.java_position, 4) |
paul@8 | 1369 | to_boundary = (4 - r) % 4 |
paul@8 | 1370 | # Get the pertinent arguments. |
paul@8 | 1371 | arguments = arguments[to_boundary:] |
paul@8 | 1372 | default = (arguments[0] << 24) + (arguments[1] << 16) + (arguments[2] << 8) + arguments[3] |
paul@8 | 1373 | low = (arguments[4] << 24) + (arguments[5] << 16) + (arguments[6] << 8) + arguments[7] |
paul@8 | 1374 | high = (arguments[8] << 24) + (arguments[9] << 16) + (arguments[10] << 8) + arguments[11] |
paul@8 | 1375 | # Process the jump entries. |
paul@8 | 1376 | # NOTE: This is not the most optimal implementation. |
paul@8 | 1377 | jump_index = 8 |
paul@8 | 1378 | for jump in range(low, high + 1): |
paul@8 | 1379 | offset = signed4((arguments[jump_index] << 24) + (arguments[jump_index + 1] << 16) + |
paul@8 | 1380 | (arguments[jump_index + 2] << 8) + arguments[jump_index + 3]) |
paul@8 | 1381 | # Calculate the branch target. |
paul@8 | 1382 | java_absolute = self.java_position + offset |
paul@8 | 1383 | # Generate branching code. |
paul@8 | 1384 | program.dup_top() # Stack: key, key |
paul@8 | 1385 | program.load_const(jump) # Stack: key, key, jump |
paul@8 | 1386 | program.compare_op("==") # Stack: key, result |
paul@8 | 1387 | program.jump_to_label(0, "end") |
paul@8 | 1388 | program.pop_top() # Stack: key |
paul@8 | 1389 | program.pop_top() # Stack: |
paul@8 | 1390 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@8 | 1391 | # Generate the label for the end of the branching code. |
paul@8 | 1392 | program.start_label("end") |
paul@8 | 1393 | program.pop_top() # Stack: key |
paul@8 | 1394 | # Update the index. |
paul@8 | 1395 | jump_index += 8 |
paul@8 | 1396 | # Generate the default. |
paul@8 | 1397 | java_absolute = self.java_position + default |
paul@8 | 1398 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@7 | 1399 | |
paul@7 | 1400 | def wide(self, code, program): |
paul@7 | 1401 | # NOTE: To be implemented. |
paul@7 | 1402 | return number_of_arguments |
paul@7 | 1403 | |
paul@7 | 1404 | def disassemble(class_file, code): |
paul@7 | 1405 | disassembler = BytecodeDisassembler(class_file) |
paul@7 | 1406 | disassembler.process(code, BytecodeDisassemblerProgram()) |
paul@7 | 1407 | |
paul@7 | 1408 | def translate(class_file, code): |
paul@7 | 1409 | translator = BytecodeTranslator(class_file) |
paul@7 | 1410 | writer = BytecodeWriter() |
paul@7 | 1411 | translator.process(code, writer) |
paul@7 | 1412 | return writer |
paul@7 | 1413 | |
paul@5 | 1414 | if __name__ == "__main__": |
paul@5 | 1415 | import sys |
paul@5 | 1416 | from classfile import ClassFile |
paul@5 | 1417 | f = open(sys.argv[1]) |
paul@5 | 1418 | c = ClassFile(f.read()) |
paul@4 | 1419 | |
paul@4 | 1420 | # vim: tabstop=4 expandtab shiftwidth=4 |