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