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