paul@181 | 1 | #!/usr/bin/env python |
paul@181 | 2 | |
paul@181 | 3 | """ |
paul@181 | 4 | Java bytecode conversion. Specification found at the following URL: |
paul@181 | 5 | http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc.html |
paul@181 | 6 | |
paul@181 | 7 | NOTE: Synchronized constructs are not actually supported. |
paul@181 | 8 | """ |
paul@181 | 9 | |
paul@181 | 10 | import classfile |
paul@181 | 11 | from dis import cmp_op, opname # for access to Python bytecode values and operators |
paul@181 | 12 | try: |
paul@181 | 13 | from dis import opmap |
paul@181 | 14 | except ImportError: |
paul@181 | 15 | opmap = {} |
paul@181 | 16 | for i in range(0, len(opname)): |
paul@181 | 17 | opmap[opname[i]] = i |
paul@181 | 18 | from UserDict import UserDict |
paul@181 | 19 | import new |
paul@181 | 20 | |
paul@181 | 21 | # Bytecode production classes. |
paul@181 | 22 | |
paul@181 | 23 | class BytecodeWriter: |
paul@181 | 24 | |
paul@181 | 25 | "A Python bytecode writer." |
paul@181 | 26 | |
paul@181 | 27 | def __init__(self): |
paul@181 | 28 | |
paul@181 | 29 | "Initialise the writer." |
paul@181 | 30 | |
paul@181 | 31 | # A stack of loop start instructions corresponding to loop blocks. |
paul@181 | 32 | self.loops = [] |
paul@181 | 33 | |
paul@181 | 34 | # A stack of loop block or exception block start positions. |
paul@181 | 35 | self.blocks = [] |
paul@181 | 36 | |
paul@181 | 37 | # A stack of exception block handler pointers. |
paul@181 | 38 | self.exception_handlers = [] |
paul@181 | 39 | |
paul@181 | 40 | # A list of exception offset details. |
paul@181 | 41 | self.exception_offsets = [] |
paul@181 | 42 | |
paul@181 | 43 | # A dictionary mapping labels to jump instructions referencing such labels. |
paul@181 | 44 | self.jumps = {} |
paul@181 | 45 | |
paul@181 | 46 | # The output values, including "lazy" subvalues which will need evaluating. |
paul@181 | 47 | self.output = [] |
paul@181 | 48 | |
paul@181 | 49 | # The current Python bytecode instruction position. |
paul@181 | 50 | self.position = 0 |
paul@181 | 51 | |
paul@181 | 52 | # Stack depth estimation. |
paul@181 | 53 | self.stack_depth = 0 |
paul@181 | 54 | self.max_stack_depth = 0 |
paul@181 | 55 | |
paul@181 | 56 | # Local variable estimation. |
paul@181 | 57 | self.max_locals = 0 |
paul@181 | 58 | |
paul@181 | 59 | # Mapping from values to indexes. |
paul@181 | 60 | self.constants = {} |
paul@181 | 61 | |
paul@181 | 62 | # Mapping from names to indexes. |
paul@181 | 63 | # NOTE: This may be acquired from elsewhere. |
paul@181 | 64 | #self.globals = {} |
paul@181 | 65 | |
paul@181 | 66 | # Mapping from names to indexes. |
paul@181 | 67 | self.names = {} |
paul@181 | 68 | |
paul@181 | 69 | # A list of constants used as exception handler return addresses. |
paul@181 | 70 | self.constants_for_exceptions = [] |
paul@181 | 71 | |
paul@181 | 72 | # A list of external names. |
paul@181 | 73 | self.external_names = [] |
paul@181 | 74 | |
paul@181 | 75 | def get_bytecodes(self): |
paul@181 | 76 | |
paul@181 | 77 | "Return the list of bytecodes written to the writer." |
paul@181 | 78 | |
paul@181 | 79 | output = [] |
paul@181 | 80 | for element in self.output: |
paul@181 | 81 | if isinstance(element, LazySubValue): |
paul@181 | 82 | value = element.value |
paul@181 | 83 | else: |
paul@181 | 84 | value = element |
paul@181 | 85 | output.append(value) |
paul@181 | 86 | return output |
paul@181 | 87 | |
paul@181 | 88 | def get_output(self): |
paul@181 | 89 | |
paul@181 | 90 | "Return the output of the writer as a string." |
paul@181 | 91 | |
paul@181 | 92 | output = [] |
paul@181 | 93 | for value in self.get_bytecodes(): |
paul@181 | 94 | # NOTE: ValueError gets raised for bad values here. |
paul@181 | 95 | output.append(chr(value)) |
paul@181 | 96 | return "".join(output) |
paul@181 | 97 | |
paul@181 | 98 | def get_constants(self): |
paul@181 | 99 | |
paul@181 | 100 | """ |
paul@181 | 101 | Return a list of constants with ordering significant to the code |
paul@181 | 102 | employing them. |
paul@181 | 103 | """ |
paul@181 | 104 | |
paul@181 | 105 | l = self._get_list(self._invert(self.constants)) |
paul@181 | 106 | result = [] |
paul@181 | 107 | for i in l: |
paul@181 | 108 | if isinstance(i, LazyValue): |
paul@181 | 109 | result.append(i.get_value()) |
paul@181 | 110 | else: |
paul@181 | 111 | result.append(i) |
paul@181 | 112 | return result |
paul@181 | 113 | |
paul@181 | 114 | #def get_globals(self): |
paul@181 | 115 | # return self._get_list(self._invert(self.globals)) |
paul@181 | 116 | |
paul@181 | 117 | def get_names(self): |
paul@181 | 118 | |
paul@181 | 119 | """ |
paul@181 | 120 | Return a list of names with ordering significant to the code employing |
paul@181 | 121 | them. |
paul@181 | 122 | """ |
paul@181 | 123 | |
paul@181 | 124 | return self._get_list(self._invert(self.names)) |
paul@181 | 125 | |
paul@181 | 126 | def _invert(self, d): |
paul@181 | 127 | |
paul@181 | 128 | """ |
paul@181 | 129 | Return a new dictionary whose key-to-value mapping is in the inverse of |
paul@181 | 130 | that found in 'd'. |
paul@181 | 131 | """ |
paul@181 | 132 | |
paul@181 | 133 | inverted = {} |
paul@181 | 134 | for k, v in d.items(): |
paul@181 | 135 | inverted[v] = k |
paul@181 | 136 | return inverted |
paul@181 | 137 | |
paul@181 | 138 | def _get_list(self, d): |
paul@181 | 139 | |
paul@181 | 140 | """ |
paul@181 | 141 | Traverse the dictionary 'd' returning a list whose values appear at the |
paul@181 | 142 | position denoted by each value's key in 'd'. |
paul@181 | 143 | """ |
paul@181 | 144 | |
paul@181 | 145 | l = [] |
paul@181 | 146 | for i in range(0, len(d.keys())): |
paul@181 | 147 | l.append(d[i]) |
paul@181 | 148 | return l |
paul@181 | 149 | |
paul@181 | 150 | # Administrative methods. |
paul@181 | 151 | |
paul@181 | 152 | def update_stack_depth(self, change): |
paul@181 | 153 | |
paul@181 | 154 | """ |
paul@181 | 155 | Given the stated 'change' in stack depth, update the maximum stack depth |
paul@181 | 156 | where appropriate. |
paul@181 | 157 | """ |
paul@181 | 158 | |
paul@181 | 159 | self.stack_depth += change |
paul@181 | 160 | if self.stack_depth > self.max_stack_depth: |
paul@181 | 161 | self.max_stack_depth = self.stack_depth |
paul@181 | 162 | |
paul@181 | 163 | def update_locals(self, index): |
paul@181 | 164 | |
paul@181 | 165 | """ |
paul@181 | 166 | Given the stated 'index' of a local variable, update the maximum local |
paul@181 | 167 | variable index where appropriate. |
paul@181 | 168 | """ |
paul@181 | 169 | |
paul@181 | 170 | if index > self.max_locals: |
paul@181 | 171 | self.max_locals = index |
paul@181 | 172 | |
paul@181 | 173 | # Special methods. |
paul@181 | 174 | |
paul@181 | 175 | def _write_value(self, value): |
paul@181 | 176 | |
paul@181 | 177 | """ |
paul@181 | 178 | Write the given 'value' at the current output position. |
paul@181 | 179 | """ |
paul@181 | 180 | |
paul@181 | 181 | if isinstance(value, LazyValue): |
paul@181 | 182 | # NOTE: Assume a 16-bit value. |
paul@181 | 183 | self.output.append(value.values[0]) |
paul@181 | 184 | self.output.append(value.values[1]) |
paul@181 | 185 | self.position += 2 |
paul@181 | 186 | elif value <= 0xffff: |
paul@181 | 187 | self.output.append(value & 0xff) |
paul@181 | 188 | self.output.append((value & 0xff00) >> 8) |
paul@181 | 189 | self.position += 2 |
paul@181 | 190 | else: |
paul@181 | 191 | # NOTE: EXTENDED_ARG not yet supported. |
paul@181 | 192 | raise ValueError, value |
paul@181 | 193 | |
paul@181 | 194 | def _rewrite_value(self, position, value): |
paul@181 | 195 | |
paul@181 | 196 | """ |
paul@181 | 197 | At the given output 'position', rewrite the given 'value'. |
paul@181 | 198 | """ |
paul@181 | 199 | |
paul@181 | 200 | # NOTE: Assume a 16-bit value. |
paul@181 | 201 | if value <= 0xffff: |
paul@181 | 202 | self.output[position] = (value & 0xff) |
paul@181 | 203 | self.output[position + 1] = ((value & 0xff00) >> 8) |
paul@181 | 204 | else: |
paul@181 | 205 | # NOTE: EXTENDED_ARG not yet supported. |
paul@181 | 206 | raise ValueError, value |
paul@181 | 207 | |
paul@181 | 208 | # Higher level methods. |
paul@181 | 209 | |
paul@181 | 210 | def use_external_name(self, name): |
paul@181 | 211 | # NOTE: Remove array and object indicators. |
paul@181 | 212 | self.external_names.append(name) |
paul@181 | 213 | |
paul@181 | 214 | def setup_loop(self): |
paul@181 | 215 | self.loops.append(self.position) |
paul@181 | 216 | self.output.append(opmap["SETUP_LOOP"]) |
paul@181 | 217 | self.position += 1 |
paul@181 | 218 | self._write_value(0) # To be filled in later |
paul@181 | 219 | |
paul@181 | 220 | def end_loop(self): |
paul@181 | 221 | current_loop_start = self.loops.pop() |
paul@181 | 222 | current_loop_real_start = self.blocks.pop() |
paul@181 | 223 | #print "<", self.blocks, current_loop_real_start |
paul@181 | 224 | # Fix the iterator delta. |
paul@181 | 225 | # NOTE: Using 3 as the assumed length of the FOR_ITER instruction. |
paul@181 | 226 | self.jump_absolute(current_loop_real_start) |
paul@181 | 227 | self._rewrite_value(current_loop_real_start + 1, self.position - current_loop_real_start - 3) |
paul@181 | 228 | self.pop_block() |
paul@181 | 229 | # Fix the loop delta. |
paul@181 | 230 | # NOTE: Using 3 as the assumed length of the SETUP_LOOP instruction. |
paul@181 | 231 | self._rewrite_value(current_loop_start + 1, self.position - current_loop_start - 3) |
paul@181 | 232 | |
paul@181 | 233 | def jump_to_label(self, status, name): |
paul@181 | 234 | # Record the instruction using the jump. |
paul@181 | 235 | jump_instruction = self.position |
paul@181 | 236 | if status is None: |
paul@181 | 237 | self.jump_forward() |
paul@181 | 238 | elif status: |
paul@181 | 239 | self.jump_if_true() |
paul@181 | 240 | else: |
paul@181 | 241 | self.jump_if_false() |
paul@181 | 242 | # Record the following instruction, too. |
paul@181 | 243 | if not self.jumps.has_key(name): |
paul@181 | 244 | self.jumps[name] = [] |
paul@181 | 245 | self.jumps[name].append((jump_instruction, self.position)) |
paul@181 | 246 | |
paul@181 | 247 | def start_label(self, name): |
paul@181 | 248 | # Fill in all jump instructions. |
paul@181 | 249 | for jump_instruction, following_instruction in self.jumps[name]: |
paul@181 | 250 | self._rewrite_value(jump_instruction + 1, self.position - following_instruction) |
paul@181 | 251 | del self.jumps[name] |
paul@181 | 252 | |
paul@181 | 253 | def load_const_ret(self, value): |
paul@181 | 254 | self.constants_for_exceptions.append(value) |
paul@181 | 255 | self.load_const(value) |
paul@181 | 256 | |
paul@181 | 257 | def ret(self, index): |
paul@181 | 258 | self.load_fast(index) |
paul@181 | 259 | |
paul@181 | 260 | # Previously, the constant stored on the stack by jsr/jsr_w was stored |
paul@181 | 261 | # in a local variable. In the JVM, extracting the value from the local |
paul@181 | 262 | # variable and jumping can be done at runtime. In the Python VM, any |
paul@181 | 263 | # jump target must be known in advance and written into the bytecode. |
paul@181 | 264 | |
paul@181 | 265 | for constant in self.constants_for_exceptions: |
paul@181 | 266 | self.dup_top() # Stack: actual-address, actual-address |
paul@181 | 267 | self.load_const(constant) # Stack: actual-address, actual-address, suggested-address |
paul@181 | 268 | self.compare_op("==") # Stack: actual-address, result |
paul@181 | 269 | self.jump_to_label(0, "const") |
paul@181 | 270 | self.pop_top() # Stack: actual-address |
paul@181 | 271 | self.pop_top() # Stack: |
paul@181 | 272 | self.jump_absolute(constant) |
paul@181 | 273 | self.start_label("const") |
paul@181 | 274 | self.pop_top() # Stack: actual-address |
paul@181 | 275 | |
paul@181 | 276 | # NOTE: If we get here, something is really wrong. |
paul@181 | 277 | |
paul@181 | 278 | self.pop_top() # Stack: |
paul@181 | 279 | |
paul@181 | 280 | def setup_except(self, target): |
paul@181 | 281 | self.blocks.append(self.position) |
paul@181 | 282 | self.exception_handlers.append(target) |
paul@181 | 283 | #print "-", self.position, target |
paul@181 | 284 | self.output.append(opmap["SETUP_EXCEPT"]) |
paul@181 | 285 | self.position += 1 |
paul@181 | 286 | self._write_value(0) # To be filled in later |
paul@181 | 287 | |
paul@181 | 288 | def setup_finally(self, target): |
paul@181 | 289 | self.blocks.append(self.position) |
paul@181 | 290 | self.exception_handlers.append(target) |
paul@181 | 291 | #print "-", self.position, target |
paul@181 | 292 | self.output.append(opmap["SETUP_FINALLY"]) |
paul@181 | 293 | self.position += 1 |
paul@181 | 294 | self._write_value(0) # To be filled in later |
paul@181 | 295 | |
paul@181 | 296 | def end_exception(self): |
paul@181 | 297 | current_exception_start = self.blocks.pop() |
paul@181 | 298 | # Convert the "lazy" absolute value. |
paul@181 | 299 | current_exception_target = self.exception_handlers.pop() |
paul@181 | 300 | # NOTE: Using 3 as the assumed length of the SETUP_* instruction. |
paul@181 | 301 | self.exception_offsets.append((current_exception_start + 1, current_exception_target, current_exception_start)) |
paul@181 | 302 | |
paul@181 | 303 | def end_exceptions(self): |
paul@181 | 304 | for position, exception_target, exception_start in self.exception_offsets: |
paul@181 | 305 | #print "*", exception_start, exception_target.get_value() |
paul@181 | 306 | self._rewrite_value(position, exception_target.get_value() - exception_start - 3) |
paul@181 | 307 | |
paul@181 | 308 | def start_handler(self, exc_name, class_file): |
paul@181 | 309 | |
paul@181 | 310 | # Where handlers are begun, produce bytecode to test the type of |
paul@181 | 311 | # the exception. |
paul@181 | 312 | # NOTE: Since RAISE_VARARGS and END_FINALLY are not really documented, |
paul@181 | 313 | # NOTE: we store the top of the stack and use it later to trigger the |
paul@181 | 314 | # NOTE: magic processes when re-raising. |
paul@181 | 315 | self.use_external_name(str(exc_name)) |
paul@181 | 316 | |
paul@181 | 317 | self.rot_two() # Stack: raised-exception, exception |
paul@181 | 318 | self.dup_top() # Stack: raised-exception, exception, exception |
paul@181 | 319 | # Handled exceptions are wrapped before being thrown. |
paul@181 | 320 | self.load_global("Exception") # Stack: raised-exception, exception, exception, Exception |
paul@181 | 321 | self.compare_op("exception match") # Stack: raised-exception, exception, result |
paul@181 | 322 | self.jump_to_label(0, "next") |
paul@181 | 323 | self.pop_top() # Stack: raised-exception, exception |
paul@181 | 324 | self.dup_top() # Stack: raised-exception, exception, exception |
paul@181 | 325 | self.load_attr("args") # Stack: raised-exception, exception, args |
paul@181 | 326 | self.load_const(0) # Stack: raised-exception, exception, args, 0 |
paul@181 | 327 | self.binary_subscr() # Stack: raised-exception, exception, exception-object |
paul@181 | 328 | load_class_name(class_file, str(exc_name), self) |
paul@181 | 329 | # Stack: raised-exception, exception, exception-object, handled-exception |
paul@181 | 330 | self.load_global("isinstance") # Stack: raised-exception, exception, exception-object, handled-exception, isinstance |
paul@181 | 331 | self.rot_three() # Stack: raised-exception, exception, isinstance, exception-object, handled-exception |
paul@181 | 332 | self.call_function(2) # Stack: raised-exception, exception, result |
paul@181 | 333 | self.jump_to_label(1, "handler") |
paul@181 | 334 | self.start_label("next") |
paul@181 | 335 | self.pop_top() # Stack: raised-exception, exception |
paul@181 | 336 | self.rot_two() # Stack: exception, raised-exception |
paul@181 | 337 | self.end_finally() |
paul@181 | 338 | self.start_label("handler") |
paul@181 | 339 | self.pop_top() # Stack: raised-exception, exception |
paul@181 | 340 | |
paul@181 | 341 | # Complicated methods. |
paul@181 | 342 | |
paul@181 | 343 | def load_const(self, value): |
paul@181 | 344 | self.output.append(opmap["LOAD_CONST"]) |
paul@181 | 345 | if not self.constants.has_key(value): |
paul@181 | 346 | self.constants[value] = len(self.constants.keys()) |
paul@181 | 347 | self.position += 1 |
paul@181 | 348 | self._write_value(self.constants[value]) |
paul@181 | 349 | self.update_stack_depth(1) |
paul@181 | 350 | |
paul@181 | 351 | def load_global(self, name): |
paul@181 | 352 | self.output.append(opmap["LOAD_GLOBAL"]) |
paul@181 | 353 | if not self.names.has_key(name): |
paul@181 | 354 | self.names[name] = len(self.names.keys()) |
paul@181 | 355 | self.position += 1 |
paul@181 | 356 | self._write_value(self.names[name]) |
paul@181 | 357 | self.update_stack_depth(1) |
paul@181 | 358 | |
paul@181 | 359 | def load_attr(self, name): |
paul@181 | 360 | self.output.append(opmap["LOAD_ATTR"]) |
paul@181 | 361 | if not self.names.has_key(name): |
paul@181 | 362 | self.names[name] = len(self.names.keys()) |
paul@181 | 363 | self.position += 1 |
paul@181 | 364 | self._write_value(self.names[name]) |
paul@181 | 365 | |
paul@181 | 366 | def load_name(self, name): |
paul@181 | 367 | self.output.append(opmap["LOAD_NAME"]) |
paul@181 | 368 | if not self.names.has_key(name): |
paul@181 | 369 | self.names[name] = len(self.names.keys()) |
paul@181 | 370 | self.position += 1 |
paul@181 | 371 | self._write_value(self.names[name]) |
paul@181 | 372 | self.update_stack_depth(1) |
paul@181 | 373 | |
paul@181 | 374 | def load_fast(self, index): |
paul@181 | 375 | self.output.append(opmap["LOAD_FAST"]) |
paul@181 | 376 | self.position += 1 |
paul@181 | 377 | self._write_value(index) |
paul@181 | 378 | self.update_stack_depth(1) |
paul@181 | 379 | self.update_locals(index) |
paul@181 | 380 | |
paul@181 | 381 | def store_attr(self, name): |
paul@181 | 382 | self.output.append(opmap["STORE_ATTR"]) |
paul@181 | 383 | if not self.names.has_key(name): |
paul@181 | 384 | self.names[name] = len(self.names.keys()) |
paul@181 | 385 | self.position += 1 |
paul@181 | 386 | self._write_value(self.names[name]) |
paul@181 | 387 | self.update_stack_depth(-1) |
paul@181 | 388 | |
paul@181 | 389 | def store_fast(self, index): |
paul@181 | 390 | self.output.append(opmap["STORE_FAST"]) |
paul@181 | 391 | self.position += 1 |
paul@181 | 392 | self._write_value(index) |
paul@181 | 393 | self.update_stack_depth(-1) |
paul@181 | 394 | self.update_locals(index) |
paul@181 | 395 | |
paul@181 | 396 | def for_iter(self): |
paul@181 | 397 | self.blocks.append(self.position) |
paul@181 | 398 | #print ">", self.blocks |
paul@181 | 399 | self.output.append(opmap["FOR_ITER"]) |
paul@181 | 400 | self.position += 1 |
paul@181 | 401 | self._write_value(0) # To be filled in later |
paul@181 | 402 | self.update_stack_depth(1) |
paul@181 | 403 | |
paul@181 | 404 | def break_loop(self): |
paul@181 | 405 | self.output.append(opmap["BREAK_LOOP"]) |
paul@181 | 406 | self.position += 1 |
paul@181 | 407 | self.jump_absolute(self.blocks[-1]) |
paul@181 | 408 | |
paul@181 | 409 | # Normal bytecode generators. |
paul@181 | 410 | |
paul@181 | 411 | def get_iter(self): |
paul@181 | 412 | self.output.append(opmap["GET_ITER"]) |
paul@181 | 413 | self.position += 1 |
paul@181 | 414 | |
paul@181 | 415 | def jump_if_false(self, offset=0): |
paul@181 | 416 | self.output.append(opmap["JUMP_IF_FALSE"]) |
paul@181 | 417 | self.position += 1 |
paul@181 | 418 | self._write_value(offset) # May be filled in later |
paul@181 | 419 | |
paul@181 | 420 | def jump_if_true(self, offset=0): |
paul@181 | 421 | self.output.append(opmap["JUMP_IF_TRUE"]) |
paul@181 | 422 | self.position += 1 |
paul@181 | 423 | self._write_value(offset) # May be filled in later |
paul@181 | 424 | |
paul@181 | 425 | def jump_forward(self, offset=0): |
paul@181 | 426 | self.output.append(opmap["JUMP_FORWARD"]) |
paul@181 | 427 | self.position += 1 |
paul@181 | 428 | self._write_value(offset) # May be filled in later |
paul@181 | 429 | |
paul@181 | 430 | def jump_absolute(self, address=0): |
paul@181 | 431 | self.output.append(opmap["JUMP_ABSOLUTE"]) |
paul@181 | 432 | self.position += 1 |
paul@181 | 433 | self._write_value(address) # May be filled in later |
paul@181 | 434 | |
paul@181 | 435 | def build_tuple(self, count): |
paul@181 | 436 | self.output.append(opmap["BUILD_TUPLE"]) |
paul@181 | 437 | self.position += 1 |
paul@181 | 438 | self._write_value(count) |
paul@181 | 439 | self.update_stack_depth(-(count - 1)) |
paul@181 | 440 | |
paul@181 | 441 | def build_list(self, count): |
paul@181 | 442 | self.output.append(opmap["BUILD_LIST"]) |
paul@181 | 443 | self.position += 1 |
paul@181 | 444 | self._write_value(count) |
paul@181 | 445 | self.update_stack_depth(-(count - 1)) |
paul@181 | 446 | |
paul@181 | 447 | def pop_top(self): |
paul@181 | 448 | self.output.append(opmap["POP_TOP"]) |
paul@181 | 449 | self.position += 1 |
paul@181 | 450 | self.update_stack_depth(-1) |
paul@181 | 451 | |
paul@181 | 452 | def dup_top(self): |
paul@181 | 453 | self.output.append(opmap["DUP_TOP"]) |
paul@181 | 454 | self.position += 1 |
paul@181 | 455 | self.update_stack_depth(1) |
paul@181 | 456 | |
paul@181 | 457 | def dup_topx(self, count): |
paul@181 | 458 | self.output.append(opmap["DUP_TOPX"]) |
paul@181 | 459 | self.position += 1 |
paul@181 | 460 | self._write_value(count) |
paul@181 | 461 | self.update_stack_depth(count) |
paul@181 | 462 | |
paul@181 | 463 | def rot_two(self): |
paul@181 | 464 | self.output.append(opmap["ROT_TWO"]) |
paul@181 | 465 | self.position += 1 |
paul@181 | 466 | |
paul@181 | 467 | def rot_three(self): |
paul@181 | 468 | self.output.append(opmap["ROT_THREE"]) |
paul@181 | 469 | self.position += 1 |
paul@181 | 470 | |
paul@181 | 471 | def rot_four(self): |
paul@181 | 472 | self.output.append(opmap["ROT_FOUR"]) |
paul@181 | 473 | self.position += 1 |
paul@181 | 474 | |
paul@181 | 475 | def call_function(self, count): |
paul@181 | 476 | self.output.append(opmap["CALL_FUNCTION"]) |
paul@181 | 477 | self.position += 1 |
paul@181 | 478 | self._write_value(count) |
paul@181 | 479 | self.update_stack_depth(-count) |
paul@181 | 480 | |
paul@181 | 481 | def call_function_var(self, count): |
paul@181 | 482 | self.output.append(opmap["CALL_FUNCTION_VAR"]) |
paul@181 | 483 | self.position += 1 |
paul@181 | 484 | self._write_value(count) |
paul@181 | 485 | self.update_stack_depth(-count-1) |
paul@181 | 486 | |
paul@181 | 487 | def binary_subscr(self): |
paul@181 | 488 | self.output.append(opmap["BINARY_SUBSCR"]) |
paul@181 | 489 | self.position += 1 |
paul@181 | 490 | self.update_stack_depth(-1) |
paul@181 | 491 | |
paul@181 | 492 | def binary_add(self): |
paul@181 | 493 | self.output.append(opmap["BINARY_ADD"]) |
paul@181 | 494 | self.position += 1 |
paul@181 | 495 | self.update_stack_depth(-1) |
paul@181 | 496 | |
paul@181 | 497 | def binary_divide(self): |
paul@181 | 498 | self.output.append(opmap["BINARY_DIVIDE"]) |
paul@181 | 499 | self.position += 1 |
paul@181 | 500 | self.update_stack_depth(-1) |
paul@181 | 501 | |
paul@181 | 502 | def binary_multiply(self): |
paul@181 | 503 | self.output.append(opmap["BINARY_MULTIPLY"]) |
paul@181 | 504 | self.position += 1 |
paul@181 | 505 | self.update_stack_depth(-1) |
paul@181 | 506 | |
paul@181 | 507 | def binary_modulo(self): |
paul@181 | 508 | self.output.append(opmap["BINARY_MODULO"]) |
paul@181 | 509 | self.position += 1 |
paul@181 | 510 | self.update_stack_depth(-1) |
paul@181 | 511 | |
paul@181 | 512 | def binary_subtract(self): |
paul@181 | 513 | self.output.append(opmap["BINARY_SUBTRACT"]) |
paul@181 | 514 | self.position += 1 |
paul@181 | 515 | self.update_stack_depth(-1) |
paul@181 | 516 | |
paul@181 | 517 | def binary_and(self): |
paul@181 | 518 | self.output.append(opmap["BINARY_AND"]) |
paul@181 | 519 | self.position += 1 |
paul@181 | 520 | self.update_stack_depth(-1) |
paul@181 | 521 | |
paul@181 | 522 | def binary_or(self): |
paul@181 | 523 | self.output.append(opmap["BINARY_XOR"]) |
paul@181 | 524 | self.position += 1 |
paul@181 | 525 | self.update_stack_depth(-1) |
paul@181 | 526 | |
paul@181 | 527 | def binary_lshift(self): |
paul@181 | 528 | self.output.append(opmap["BINARY_LSHIFT"]) |
paul@181 | 529 | self.position += 1 |
paul@181 | 530 | self.update_stack_depth(-1) |
paul@181 | 531 | |
paul@181 | 532 | def binary_rshift(self): |
paul@181 | 533 | self.output.append(opmap["BINARY_RSHIFT"]) |
paul@181 | 534 | self.position += 1 |
paul@181 | 535 | self.update_stack_depth(-1) |
paul@181 | 536 | |
paul@181 | 537 | def binary_xor(self): |
paul@181 | 538 | self.output.append(opmap["BINARY_XOR"]) |
paul@181 | 539 | self.position += 1 |
paul@181 | 540 | self.update_stack_depth(-1) |
paul@181 | 541 | |
paul@181 | 542 | def store_subscr(self): |
paul@181 | 543 | self.output.append(opmap["STORE_SUBSCR"]) |
paul@181 | 544 | self.position += 1 |
paul@181 | 545 | self.update_stack_depth(-3) |
paul@181 | 546 | |
paul@181 | 547 | def unary_negative(self): |
paul@181 | 548 | self.output.append(opmap["UNARY_NEGATIVE"]) |
paul@181 | 549 | self.position += 1 |
paul@181 | 550 | |
paul@181 | 551 | def slice_0(self): |
paul@181 | 552 | self.output.append(opmap["SLICE+0"]) |
paul@181 | 553 | self.position += 1 |
paul@181 | 554 | |
paul@181 | 555 | def slice_1(self): |
paul@181 | 556 | self.output.append(opmap["SLICE+1"]) |
paul@181 | 557 | self.position += 1 |
paul@181 | 558 | |
paul@181 | 559 | def compare_op(self, op): |
paul@181 | 560 | self.output.append(opmap["COMPARE_OP"]) |
paul@181 | 561 | self.position += 1 |
paul@181 | 562 | self._write_value(list(cmp_op).index(op)) |
paul@181 | 563 | self.update_stack_depth(-1) |
paul@181 | 564 | |
paul@181 | 565 | def return_value(self): |
paul@181 | 566 | self.output.append(opmap["RETURN_VALUE"]) |
paul@181 | 567 | self.position += 1 |
paul@181 | 568 | self.update_stack_depth(-1) |
paul@181 | 569 | |
paul@181 | 570 | def raise_varargs(self, count): |
paul@181 | 571 | self.output.append(opmap["RAISE_VARARGS"]) |
paul@181 | 572 | self.position += 1 |
paul@181 | 573 | self._write_value(count) |
paul@181 | 574 | |
paul@181 | 575 | def pop_block(self): |
paul@181 | 576 | self.output.append(opmap["POP_BLOCK"]) |
paul@181 | 577 | self.position += 1 |
paul@181 | 578 | |
paul@181 | 579 | def end_finally(self): |
paul@181 | 580 | self.output.append(opmap["END_FINALLY"]) |
paul@181 | 581 | self.position += 1 |
paul@181 | 582 | |
paul@181 | 583 | def unpack_sequence(self, count): |
paul@181 | 584 | self.output.append(opmap["UNPACK_SEQUENCE"]) |
paul@181 | 585 | self.position += 1 |
paul@181 | 586 | self._write_value(count) |
paul@181 | 587 | |
paul@181 | 588 | # Debugging. |
paul@181 | 589 | |
paul@181 | 590 | def print_item(self): |
paul@181 | 591 | self.output.append(opmap["PRINT_ITEM"]) |
paul@181 | 592 | self.position += 1 |
paul@181 | 593 | |
paul@181 | 594 | # Utility classes and functions. |
paul@181 | 595 | |
paul@181 | 596 | class LazyDict(UserDict): |
paul@181 | 597 | def __getitem__(self, key): |
paul@181 | 598 | if not self.data.has_key(key): |
paul@181 | 599 | # NOTE: Assume 16-bit value. |
paul@181 | 600 | self.data[key] = LazyValue(2) |
paul@181 | 601 | return self.data[key] |
paul@181 | 602 | def __setitem__(self, key, value): |
paul@181 | 603 | if self.data.has_key(key): |
paul@181 | 604 | existing_value = self.data[key] |
paul@181 | 605 | if isinstance(existing_value, LazyValue): |
paul@181 | 606 | existing_value.set_value(value) |
paul@181 | 607 | return |
paul@181 | 608 | self.data[key] = value |
paul@181 | 609 | |
paul@181 | 610 | class LazyValue: |
paul@181 | 611 | def __init__(self, nvalues): |
paul@181 | 612 | self.values = [] |
paul@181 | 613 | for i in range(0, nvalues): |
paul@181 | 614 | self.values.append(LazySubValue()) |
paul@181 | 615 | def set_value(self, value): |
paul@181 | 616 | # NOTE: Assume at least 16-bit value. No "filling" performed. |
paul@181 | 617 | if value <= 0xffff: |
paul@181 | 618 | self.values[0].set_value(value & 0xff) |
paul@181 | 619 | self.values[1].set_value((value & 0xff00) >> 8) |
paul@181 | 620 | else: |
paul@181 | 621 | # NOTE: EXTENDED_ARG not yet supported. |
paul@181 | 622 | raise ValueError, value |
paul@181 | 623 | def get_value(self): |
paul@181 | 624 | value = 0 |
paul@181 | 625 | values = self.values[:] |
paul@181 | 626 | for i in range(0, len(values)): |
paul@181 | 627 | value = (value << 8) + values.pop().value |
paul@181 | 628 | return value |
paul@181 | 629 | |
paul@181 | 630 | class LazySubValue: |
paul@181 | 631 | def __init__(self): |
paul@181 | 632 | self.value = 0 |
paul@181 | 633 | def set_value(self, value): |
paul@181 | 634 | self.value = value |
paul@181 | 635 | |
paul@181 | 636 | def signed(value, limit): |
paul@181 | 637 | |
paul@181 | 638 | """ |
paul@181 | 639 | Return the signed integer from the unsigned 'value', where 'limit' (a value |
paul@181 | 640 | one greater than the highest possible positive integer) is used to determine |
paul@181 | 641 | whether a negative or positive result is produced. |
paul@181 | 642 | """ |
paul@181 | 643 | |
paul@181 | 644 | d, r = divmod(value, limit) |
paul@181 | 645 | if d == 1: |
paul@181 | 646 | mask = limit * 2 - 1 |
paul@181 | 647 | return -1 - (value ^ mask) |
paul@181 | 648 | else: |
paul@181 | 649 | return value |
paul@181 | 650 | |
paul@181 | 651 | def signed1(value): |
paul@181 | 652 | return signed(value, 0x80) |
paul@181 | 653 | |
paul@181 | 654 | def signed2(value): |
paul@181 | 655 | return signed(value, 0x8000) |
paul@181 | 656 | |
paul@181 | 657 | def signed4(value): |
paul@181 | 658 | return signed(value, 0x80000000) |
paul@181 | 659 | |
paul@181 | 660 | def load_class_name(class_file, full_class_name, program): |
paul@181 | 661 | this_class_name = str(class_file.this_class.get_python_name()) |
paul@181 | 662 | this_class_parts = this_class_name.split(".") |
paul@181 | 663 | class_parts = full_class_name.split(".") |
paul@181 | 664 | |
paul@181 | 665 | # Only use the full path if different from this class's path. |
paul@181 | 666 | |
paul@181 | 667 | if class_parts[:-1] != this_class_parts[:-1]: |
paul@181 | 668 | program.use_external_name(full_class_name) |
paul@181 | 669 | program.load_global(class_parts[0]) |
paul@181 | 670 | for class_part in class_parts[1:]: |
paul@181 | 671 | program.load_attr(class_part) # Stack: classref |
paul@181 | 672 | else: |
paul@181 | 673 | program.load_global(class_parts[-1]) |
paul@181 | 674 | |
paul@181 | 675 | atypes_to_default_values = { |
paul@181 | 676 | 4 : 0, # bool (NOTE: Should be False.) |
paul@181 | 677 | 5 : u"", # char |
paul@181 | 678 | 6 : 0.0, # float |
paul@181 | 679 | 7 : 0.0, # double |
paul@181 | 680 | 8 : 0, # byte |
paul@181 | 681 | 9 : 0, # short |
paul@181 | 682 | 10: 0, # int |
paul@181 | 683 | 11: 0 # long |
paul@181 | 684 | } |
paul@181 | 685 | |
paul@181 | 686 | def get_default_for_atype(atype): |
paul@181 | 687 | global atypes_to_default_values |
paul@181 | 688 | return atypes_to_default_values.get(atype) |
paul@181 | 689 | |
paul@181 | 690 | # Bytecode conversion. |
paul@181 | 691 | |
paul@181 | 692 | class BytecodeReader: |
paul@181 | 693 | |
paul@181 | 694 | "A generic Java bytecode reader." |
paul@181 | 695 | |
paul@181 | 696 | def __init__(self, class_file): |
paul@181 | 697 | |
paul@181 | 698 | """ |
paul@181 | 699 | Initialise the reader with a 'class_file' containing essential |
paul@181 | 700 | information for any bytecode inspection activity. |
paul@181 | 701 | """ |
paul@181 | 702 | |
paul@181 | 703 | self.class_file = class_file |
paul@181 | 704 | self.position_mapping = LazyDict() |
paul@181 | 705 | |
paul@181 | 706 | def process(self, method, program): |
paul@181 | 707 | |
paul@181 | 708 | """ |
paul@181 | 709 | Process the given 'method' (obtained from the class file), using the |
paul@181 | 710 | given 'program' to write translated Python bytecode instructions. |
paul@181 | 711 | """ |
paul@181 | 712 | |
paul@181 | 713 | self.java_position = 0 |
paul@181 | 714 | self.in_finally = 0 |
paul@181 | 715 | self.method = method |
paul@181 | 716 | |
paul@181 | 717 | # NOTE: Potentially unreliable way of getting necessary information. |
paul@181 | 718 | |
paul@181 | 719 | code, exception_table = None, None |
paul@181 | 720 | for attribute in method.attributes: |
paul@181 | 721 | if isinstance(attribute, classfile.CodeAttributeInfo): |
paul@181 | 722 | code, exception_table = attribute.code, attribute.exception_table |
paul@181 | 723 | break |
paul@181 | 724 | |
paul@181 | 725 | # Where no code was found, write a very simple placeholder routine. |
paul@181 | 726 | # This is useful for interfaces and abstract classes. |
paul@181 | 727 | # NOTE: Assess the correctness of doing this. An exception should really |
paul@181 | 728 | # NOTE: be raised instead. |
paul@181 | 729 | |
paul@181 | 730 | if code is None: |
paul@181 | 731 | program.load_const(None) |
paul@181 | 732 | program.return_value() |
paul@181 | 733 | return |
paul@181 | 734 | |
paul@181 | 735 | # Produce a structure which permits fast access to exception details. |
paul@181 | 736 | |
paul@181 | 737 | exception_block_start = {} |
paul@181 | 738 | exception_block_end = {} |
paul@181 | 739 | exception_block_handler = {} |
paul@181 | 740 | reversed_exception_table = exception_table[:] |
paul@181 | 741 | reversed_exception_table.reverse() |
paul@181 | 742 | |
paul@181 | 743 | # Later entries have wider coverage than earlier entries. |
paul@181 | 744 | |
paul@181 | 745 | for exception in reversed_exception_table: |
paul@181 | 746 | |
paul@181 | 747 | # NOTE: Strange case with javac from JDK 1.4 but not JDK 1.3: |
paul@181 | 748 | # NOTE: start_pc == handler_pc |
paul@181 | 749 | # Merge all finally handlers with the same handler location. |
paul@181 | 750 | |
paul@181 | 751 | if exception.catch_type == 0 and exception_block_handler.get(exception.handler_pc, []) != []: |
paul@181 | 752 | |
paul@181 | 753 | # Make a new definition. |
paul@181 | 754 | |
paul@181 | 755 | new_exception = classfile.ExceptionInfo() |
paul@181 | 756 | new_exception.catch_type = exception.catch_type |
paul@181 | 757 | new_exception.handler_pc = exception.handler_pc |
paul@181 | 758 | new_exception.end_pc = exception.end_pc |
paul@181 | 759 | new_exception.start_pc = exception.start_pc |
paul@181 | 760 | |
paul@181 | 761 | # Find the previous exception handler definition. |
paul@181 | 762 | |
paul@181 | 763 | for previous_exception in exception_block_handler[exception.handler_pc][:]: |
paul@181 | 764 | if previous_exception.catch_type == 0: |
paul@181 | 765 | new_exception.end_pc = max(new_exception.end_pc, previous_exception.end_pc) |
paul@181 | 766 | new_exception.start_pc = min(new_exception.start_pc, previous_exception.start_pc) |
paul@181 | 767 | |
paul@181 | 768 | # Remove this exception from the lists. |
paul@181 | 769 | |
paul@181 | 770 | exception_block_handler[previous_exception.handler_pc].remove(previous_exception) |
paul@181 | 771 | exception_block_start[previous_exception.start_pc].remove(previous_exception) |
paul@181 | 772 | exception_block_end[previous_exception.end_pc].remove(previous_exception) |
paul@181 | 773 | break |
paul@181 | 774 | |
paul@181 | 775 | # Use the new definition instead. |
paul@181 | 776 | |
paul@181 | 777 | exception = new_exception |
paul@181 | 778 | |
paul@181 | 779 | # Index start positions. |
paul@181 | 780 | |
paul@181 | 781 | if not exception_block_start.has_key(exception.start_pc): |
paul@181 | 782 | exception_block_start[exception.start_pc] = [] |
paul@181 | 783 | exception_block_start[exception.start_pc].append(exception) |
paul@181 | 784 | |
paul@181 | 785 | # Index end positions. |
paul@181 | 786 | |
paul@181 | 787 | if not exception_block_end.has_key(exception.end_pc): |
paul@181 | 788 | exception_block_end[exception.end_pc] = [] |
paul@181 | 789 | exception_block_end[exception.end_pc].append(exception) |
paul@181 | 790 | |
paul@181 | 791 | # Index handler positions. |
paul@181 | 792 | |
paul@181 | 793 | if not exception_block_handler.has_key(exception.handler_pc): |
paul@181 | 794 | exception_block_handler[exception.handler_pc] = [] |
paul@181 | 795 | exception_block_handler[exception.handler_pc].append(exception) |
paul@181 | 796 | |
paul@181 | 797 | # Process each instruction in the code. |
paul@181 | 798 | |
paul@181 | 799 | while self.java_position < len(code): |
paul@181 | 800 | self.position_mapping[self.java_position] = program.position |
paul@181 | 801 | |
paul@181 | 802 | # Insert exception handling constructs. |
paul@181 | 803 | |
paul@181 | 804 | block_starts = exception_block_start.get(self.java_position, []) |
paul@181 | 805 | for exception in block_starts: |
paul@181 | 806 | |
paul@181 | 807 | # Note that the absolute position is used. |
paul@181 | 808 | |
paul@181 | 809 | if exception.catch_type == 0: |
paul@181 | 810 | program.setup_finally(self.position_mapping[exception.handler_pc]) |
paul@181 | 811 | else: |
paul@181 | 812 | program.setup_except(self.position_mapping[exception.handler_pc]) |
paul@181 | 813 | |
paul@181 | 814 | if block_starts: |
paul@181 | 815 | self.in_finally = 0 |
paul@181 | 816 | |
paul@181 | 817 | # Insert exception handler details. |
paul@181 | 818 | # NOTE: Ensure that pop_block is reachable by possibly inserting it at the start of finally handlers. |
paul@181 | 819 | # NOTE: Insert a check for the correct exception at the start of each handler. |
paul@181 | 820 | |
paul@181 | 821 | for exception in exception_block_handler.get(self.java_position, []): |
paul@181 | 822 | program.end_exception() |
paul@181 | 823 | if exception.catch_type == 0: |
paul@181 | 824 | self.in_finally = 1 |
paul@181 | 825 | else: |
paul@181 | 826 | program.start_handler(self.class_file.constants[exception.catch_type - 1].get_python_name(), self.class_file) |
paul@181 | 827 | |
paul@181 | 828 | # Process the bytecode at the current position. |
paul@181 | 829 | |
paul@181 | 830 | bytecode = ord(code[self.java_position]) |
paul@181 | 831 | mnemonic, number_of_arguments = self.java_bytecodes[bytecode] |
paul@181 | 832 | number_of_arguments = self.process_bytecode(mnemonic, number_of_arguments, code, program) |
paul@181 | 833 | next_java_position = self.java_position + 1 + number_of_arguments |
paul@181 | 834 | |
paul@181 | 835 | # Insert exception block end details. |
paul@181 | 836 | |
paul@181 | 837 | for exception in exception_block_end.get(next_java_position, []): |
paul@181 | 838 | |
paul@181 | 839 | # NOTE: Insert jump beyond handlers. |
paul@181 | 840 | # NOTE: program.jump_forward/absolute(...) |
paul@181 | 841 | # NOTE: Insert end finally at end of handlers as well as where "ret" occurs. |
paul@181 | 842 | |
paul@181 | 843 | if exception.catch_type != 0: |
paul@181 | 844 | program.pop_block() |
paul@181 | 845 | |
paul@181 | 846 | # Only advance the JVM position after sneaking in extra Python |
paul@181 | 847 | # instructions. |
paul@181 | 848 | |
paul@181 | 849 | self.java_position = next_java_position |
paul@181 | 850 | |
paul@181 | 851 | # Tidy up exceptions. |
paul@181 | 852 | |
paul@181 | 853 | program.end_exceptions() |
paul@181 | 854 | |
paul@181 | 855 | def process_bytecode(self, mnemonic, number_of_arguments, code, program): |
paul@181 | 856 | |
paul@181 | 857 | """ |
paul@181 | 858 | Process a bytecode instruction with the given 'mnemonic' and |
paul@181 | 859 | 'number_of_arguments'. The 'code' parameter contains the full method |
paul@181 | 860 | code so that argument data can be inspected. The 'program' parameter is |
paul@181 | 861 | used to produce a Python translation of the instruction. |
paul@181 | 862 | """ |
paul@181 | 863 | |
paul@181 | 864 | if number_of_arguments is not None: |
paul@181 | 865 | arguments = [ord(b) for b in code[self.java_position + 1:self.java_position + 1 + number_of_arguments]] |
paul@181 | 866 | |
paul@181 | 867 | # Call the handler. |
paul@181 | 868 | |
paul@181 | 869 | getattr(self, mnemonic)(arguments, program) |
paul@181 | 870 | return number_of_arguments |
paul@181 | 871 | else: |
paul@181 | 872 | # Call the handler. |
paul@181 | 873 | |
paul@181 | 874 | return getattr(self, mnemonic)(code[self.java_position+1:], program) |
paul@181 | 875 | |
paul@181 | 876 | java_bytecodes = { |
paul@181 | 877 | # code : (mnemonic, number of following bytes, change in stack) |
paul@181 | 878 | 0 : ("nop", 0), |
paul@181 | 879 | 1 : ("aconst_null", 0), |
paul@181 | 880 | 2 : ("iconst_m1", 0), |
paul@181 | 881 | 3 : ("iconst_0", 0), |
paul@181 | 882 | 4 : ("iconst_1", 0), |
paul@181 | 883 | 5 : ("iconst_2", 0), |
paul@181 | 884 | 6 : ("iconst_3", 0), |
paul@181 | 885 | 7 : ("iconst_4", 0), |
paul@181 | 886 | 8 : ("iconst_5", 0), |
paul@181 | 887 | 9 : ("lconst_0", 0), |
paul@181 | 888 | 10 : ("lconst_1", 0), |
paul@181 | 889 | 11 : ("fconst_0", 0), |
paul@181 | 890 | 12 : ("fconst_1", 0), |
paul@181 | 891 | 13 : ("fconst_2", 0), |
paul@181 | 892 | 14 : ("dconst_0", 0), |
paul@181 | 893 | 15 : ("dconst_1", 0), |
paul@181 | 894 | 16 : ("bipush", 1), |
paul@181 | 895 | 17 : ("sipush", 2), |
paul@181 | 896 | 18 : ("ldc", 1), |
paul@181 | 897 | 19 : ("ldc_w", 2), |
paul@181 | 898 | 20 : ("ldc2_w", 2), |
paul@181 | 899 | 21 : ("iload", 1), |
paul@181 | 900 | 22 : ("lload", 1), |
paul@181 | 901 | 23 : ("fload", 1), |
paul@181 | 902 | 24 : ("dload", 1), |
paul@181 | 903 | 25 : ("aload", 1), |
paul@181 | 904 | 26 : ("iload_0", 0), |
paul@181 | 905 | 27 : ("iload_1", 0), |
paul@181 | 906 | 28 : ("iload_2", 0), |
paul@181 | 907 | 29 : ("iload_3", 0), |
paul@181 | 908 | 30 : ("lload_0", 0), |
paul@181 | 909 | 31 : ("lload_1", 0), |
paul@181 | 910 | 32 : ("lload_2", 0), |
paul@181 | 911 | 33 : ("lload_3", 0), |
paul@181 | 912 | 34 : ("fload_0", 0), |
paul@181 | 913 | 35 : ("fload_1", 0), |
paul@181 | 914 | 36 : ("fload_2", 0), |
paul@181 | 915 | 37 : ("fload_3", 0), |
paul@181 | 916 | 38 : ("dload_0", 0), |
paul@181 | 917 | 39 : ("dload_1", 0), |
paul@181 | 918 | 40 : ("dload_2", 0), |
paul@181 | 919 | 41 : ("dload_3", 0), |
paul@181 | 920 | 42 : ("aload_0", 0), |
paul@181 | 921 | 43 : ("aload_1", 0), |
paul@181 | 922 | 44 : ("aload_2", 0), |
paul@181 | 923 | 45 : ("aload_3", 0), |
paul@181 | 924 | 46 : ("iaload", 0), |
paul@181 | 925 | 47 : ("laload", 0), |
paul@181 | 926 | 48 : ("faload", 0), |
paul@181 | 927 | 49 : ("daload", 0), |
paul@181 | 928 | 50 : ("aaload", 0), |
paul@181 | 929 | 51 : ("baload", 0), |
paul@181 | 930 | 52 : ("caload", 0), |
paul@181 | 931 | 53 : ("saload", 0), |
paul@181 | 932 | 54 : ("istore", 1), |
paul@181 | 933 | 55 : ("lstore", 1), |
paul@181 | 934 | 56 : ("fstore", 1), |
paul@181 | 935 | 57 : ("dstore", 1), |
paul@181 | 936 | 58 : ("astore", 1), |
paul@181 | 937 | 59 : ("istore_0", 0), |
paul@181 | 938 | 60 : ("istore_1", 0), |
paul@181 | 939 | 61 : ("istore_2", 0), |
paul@181 | 940 | 62 : ("istore_3", 0), |
paul@181 | 941 | 63 : ("lstore_0", 0), |
paul@181 | 942 | 64 : ("lstore_1", 0), |
paul@181 | 943 | 65 : ("lstore_2", 0), |
paul@181 | 944 | 66 : ("lstore_3", 0), |
paul@181 | 945 | 67 : ("fstore_0", 0), |
paul@181 | 946 | 68 : ("fstore_1", 0), |
paul@181 | 947 | 69 : ("fstore_2", 0), |
paul@181 | 948 | 70 : ("fstore_3", 0), |
paul@181 | 949 | 71 : ("dstore_0", 0), |
paul@181 | 950 | 72 : ("dstore_1", 0), |
paul@181 | 951 | 73 : ("dstore_2", 0), |
paul@181 | 952 | 74 : ("dstore_3", 0), |
paul@181 | 953 | 75 : ("astore_0", 0), |
paul@181 | 954 | 76 : ("astore_1", 0), |
paul@181 | 955 | 77 : ("astore_2", 0), |
paul@181 | 956 | 78 : ("astore_3", 0), |
paul@181 | 957 | 79 : ("iastore", 0), |
paul@181 | 958 | 80 : ("lastore", 0), |
paul@181 | 959 | 81 : ("fastore", 0), |
paul@181 | 960 | 82 : ("dastore", 0), |
paul@181 | 961 | 83 : ("aastore", 0), |
paul@181 | 962 | 84 : ("bastore", 0), |
paul@181 | 963 | 85 : ("castore", 0), |
paul@181 | 964 | 86 : ("sastore", 0), |
paul@181 | 965 | 87 : ("pop", 0), |
paul@181 | 966 | 88 : ("pop2", 0), |
paul@181 | 967 | 89 : ("dup", 0), |
paul@181 | 968 | 90 : ("dup_x1", 0), |
paul@181 | 969 | 91 : ("dup_x2", 0), |
paul@181 | 970 | 92 : ("dup2", 0), |
paul@181 | 971 | 93 : ("dup2_x1", 0), |
paul@181 | 972 | 94 : ("dup2_x2", 0), |
paul@181 | 973 | 95 : ("swap", 0), |
paul@181 | 974 | 96 : ("iadd", 0), |
paul@181 | 975 | 97 : ("ladd", 0), |
paul@181 | 976 | 98 : ("fadd", 0), |
paul@181 | 977 | 99 : ("dadd", 0), |
paul@181 | 978 | 100 : ("isub", 0), |
paul@181 | 979 | 101 : ("lsub", 0), |
paul@181 | 980 | 102 : ("fsub", 0), |
paul@181 | 981 | 103 : ("dsub", 0), |
paul@181 | 982 | 104 : ("imul", 0), |
paul@181 | 983 | 105 : ("lmul", 0), |
paul@181 | 984 | 106 : ("fmul", 0), |
paul@181 | 985 | 107 : ("dmul", 0), |
paul@181 | 986 | 108 : ("idiv", 0), |
paul@181 | 987 | 109 : ("ldiv", 0), |
paul@181 | 988 | 110 : ("fdiv", 0), |
paul@181 | 989 | 111 : ("ddiv", 0), |
paul@181 | 990 | 112 : ("irem", 0), |
paul@181 | 991 | 113 : ("lrem", 0), |
paul@181 | 992 | 114 : ("frem", 0), |
paul@181 | 993 | 115 : ("drem", 0), |
paul@181 | 994 | 116 : ("ineg", 0), |
paul@181 | 995 | 117 : ("lneg", 0), |
paul@181 | 996 | 118 : ("fneg", 0), |
paul@181 | 997 | 119 : ("dneg", 0), |
paul@181 | 998 | 120 : ("ishl", 0), |
paul@181 | 999 | 121 : ("lshl", 0), |
paul@181 | 1000 | 122 : ("ishr", 0), |
paul@181 | 1001 | 123 : ("lshr", 0), |
paul@181 | 1002 | 124 : ("iushr", 0), |
paul@181 | 1003 | 125 : ("lushr", 0), |
paul@181 | 1004 | 126 : ("iand", 0), |
paul@181 | 1005 | 127 : ("land", 0), |
paul@181 | 1006 | 128 : ("ior", 0), |
paul@181 | 1007 | 129 : ("lor", 0), |
paul@181 | 1008 | 130 : ("ixor", 0), |
paul@181 | 1009 | 131 : ("lxor", 0), |
paul@181 | 1010 | 132 : ("iinc", 2), |
paul@181 | 1011 | 133 : ("i2l", 0), |
paul@181 | 1012 | 134 : ("i2f", 0), |
paul@181 | 1013 | 135 : ("i2d", 0), |
paul@181 | 1014 | 136 : ("l2i", 0), |
paul@181 | 1015 | 137 : ("l2f", 0), |
paul@181 | 1016 | 138 : ("l2d", 0), |
paul@181 | 1017 | 139 : ("f2i", 0), |
paul@181 | 1018 | 140 : ("f2l", 0), |
paul@181 | 1019 | 141 : ("f2d", 0), |
paul@181 | 1020 | 142 : ("d2i", 0), |
paul@181 | 1021 | 143 : ("d2l", 0), |
paul@181 | 1022 | 144 : ("d2f", 0), |
paul@181 | 1023 | 145 : ("i2b", 0), |
paul@181 | 1024 | 146 : ("i2c", 0), |
paul@181 | 1025 | 147 : ("i2s", 0), |
paul@181 | 1026 | 148 : ("lcmp", 0), |
paul@181 | 1027 | 149 : ("fcmpl", 0), |
paul@181 | 1028 | 150 : ("fcmpg", 0), |
paul@181 | 1029 | 151 : ("dcmpl", 0), |
paul@181 | 1030 | 152 : ("dcmpg", 0), |
paul@181 | 1031 | 153 : ("ifeq", 2), |
paul@181 | 1032 | 154 : ("ifne", 2), |
paul@181 | 1033 | 155 : ("iflt", 2), |
paul@181 | 1034 | 156 : ("ifge", 2), |
paul@181 | 1035 | 157 : ("ifgt", 2), |
paul@181 | 1036 | 158 : ("ifle", 2), |
paul@181 | 1037 | 159 : ("if_icmpeq", 2), |
paul@181 | 1038 | 160 : ("if_icmpne", 2), |
paul@181 | 1039 | 161 : ("if_icmplt", 2), |
paul@181 | 1040 | 162 : ("if_icmpge", 2), |
paul@181 | 1041 | 163 : ("if_icmpgt", 2), |
paul@181 | 1042 | 164 : ("if_icmple", 2), |
paul@181 | 1043 | 165 : ("if_acmpeq", 2), |
paul@181 | 1044 | 166 : ("if_acmpne", 2), |
paul@181 | 1045 | 167 : ("goto", 2), |
paul@181 | 1046 | 168 : ("jsr", 2), |
paul@181 | 1047 | 169 : ("ret", 1), |
paul@181 | 1048 | 170 : ("tableswitch", None), # variable number of arguments |
paul@181 | 1049 | 171 : ("lookupswitch", None), # variable number of arguments |
paul@181 | 1050 | 172 : ("ireturn", 0), |
paul@181 | 1051 | 173 : ("lreturn", 0), |
paul@181 | 1052 | 174 : ("freturn", 0), |
paul@181 | 1053 | 175 : ("dreturn", 0), |
paul@181 | 1054 | 176 : ("areturn", 0), |
paul@181 | 1055 | 177 : ("return_", 0), |
paul@181 | 1056 | 178 : ("getstatic", 2), |
paul@181 | 1057 | 179 : ("putstatic", 2), |
paul@181 | 1058 | 180 : ("getfield", 2), |
paul@181 | 1059 | 181 : ("putfield", 2), |
paul@181 | 1060 | 182 : ("invokevirtual", 2), |
paul@181 | 1061 | 183 : ("invokespecial", 2), |
paul@181 | 1062 | 184 : ("invokestatic", 2), |
paul@181 | 1063 | 185 : ("invokeinterface", 4), |
paul@181 | 1064 | 187 : ("new", 2), |
paul@181 | 1065 | 188 : ("newarray", 1), |
paul@181 | 1066 | 189 : ("anewarray", 2), |
paul@181 | 1067 | 190 : ("arraylength", 0), |
paul@181 | 1068 | 191 : ("athrow", 0), |
paul@181 | 1069 | 192 : ("checkcast", 2), |
paul@181 | 1070 | 193 : ("instanceof", 2), |
paul@181 | 1071 | 194 : ("monitorenter", 0), |
paul@181 | 1072 | 195 : ("monitorexit", 0), |
paul@181 | 1073 | 196 : ("wide", None), # 3 or 5 arguments, stack changes according to modified element |
paul@181 | 1074 | 197 : ("multianewarray", 3), |
paul@181 | 1075 | 198 : ("ifnull", 2), |
paul@181 | 1076 | 199 : ("ifnonnull", 2), |
paul@181 | 1077 | 200 : ("goto_w", 4), |
paul@181 | 1078 | 201 : ("jsr_w", 4), |
paul@181 | 1079 | } |
paul@181 | 1080 | |
paul@181 | 1081 | class BytecodeDisassembler(BytecodeReader): |
paul@181 | 1082 | |
paul@181 | 1083 | "A Java bytecode disassembler." |
paul@181 | 1084 | |
paul@181 | 1085 | bytecode_methods = [spec[0] for spec in BytecodeReader.java_bytecodes.values()] |
paul@181 | 1086 | |
paul@181 | 1087 | def __getattr__(self, name): |
paul@181 | 1088 | if name in self.bytecode_methods: |
paul@181 | 1089 | print "%5s %s" % (self.java_position, name), |
paul@181 | 1090 | return self.generic |
paul@181 | 1091 | else: |
paul@181 | 1092 | raise AttributeError, name |
paul@181 | 1093 | |
paul@181 | 1094 | def generic(self, arguments, program): |
paul@181 | 1095 | print arguments |
paul@181 | 1096 | |
paul@181 | 1097 | def lookupswitch(self, code, program): |
paul@181 | 1098 | print "%5s lookupswitch" % (self.java_position,), |
paul@181 | 1099 | d, r = divmod(self.java_position + 1, 4) |
paul@181 | 1100 | to_boundary = (4 - r) % 4 |
paul@181 | 1101 | code = code[to_boundary:] |
paul@181 | 1102 | default = classfile.s4(code[0:4]) |
paul@181 | 1103 | npairs = classfile.s4(code[4:8]) |
paul@181 | 1104 | print default, npairs |
paul@181 | 1105 | return to_boundary + 8 + npairs * 8 |
paul@181 | 1106 | |
paul@181 | 1107 | def tableswitch(self, code, program): |
paul@181 | 1108 | print "%5s tableswitch" % (self.java_position,), |
paul@181 | 1109 | d, r = divmod(self.java_position + 1, 4) |
paul@181 | 1110 | to_boundary = (4 - r) % 4 |
paul@181 | 1111 | code = code[to_boundary:] |
paul@181 | 1112 | default = classfile.s4(code[0:4]) |
paul@181 | 1113 | low = classfile.s4(code[4:8]) |
paul@181 | 1114 | high = classfile.s4(code[8:12]) |
paul@181 | 1115 | print default, low, high |
paul@181 | 1116 | return to_boundary + 12 + (high - low + 1) * 4 |
paul@181 | 1117 | |
paul@181 | 1118 | class BytecodeDisassemblerProgram: |
paul@181 | 1119 | position = 0 |
paul@181 | 1120 | def setup_except(self, target): |
paul@181 | 1121 | print "(setup_except %s)" % target |
paul@181 | 1122 | def setup_finally(self, target): |
paul@181 | 1123 | print "(setup_finally %s)" % target |
paul@181 | 1124 | def end_exception(self): |
paul@181 | 1125 | print "(end_exception)" |
paul@181 | 1126 | def end_exceptions(self): |
paul@181 | 1127 | print "(end_exceptions)" |
paul@181 | 1128 | def start_handler(self, exc_name, class_file): |
paul@181 | 1129 | print "(start_handler %s)" % exc_name |
paul@181 | 1130 | def pop_block(self): |
paul@181 | 1131 | print "(pop_block)" |
paul@181 | 1132 | def load_const(self, const): |
paul@181 | 1133 | print "(load_const %s)" % const |
paul@181 | 1134 | def return_value(self): |
paul@181 | 1135 | print "(return_value)" |
paul@181 | 1136 | |
paul@181 | 1137 | class BytecodeTranslator(BytecodeReader): |
paul@181 | 1138 | |
paul@181 | 1139 | "A Java bytecode translator which uses a Python bytecode writer." |
paul@181 | 1140 | |
paul@181 | 1141 | def aaload(self, arguments, program): |
paul@181 | 1142 | # NOTE: No type checking performed. |
paul@181 | 1143 | program.binary_subscr() |
paul@181 | 1144 | |
paul@181 | 1145 | def aastore(self, arguments, program): |
paul@181 | 1146 | # NOTE: No type checking performed. |
paul@181 | 1147 | # Stack: arrayref, index, value |
paul@181 | 1148 | program.rot_three() # Stack: value, arrayref, index |
paul@181 | 1149 | program.store_subscr() |
paul@181 | 1150 | |
paul@181 | 1151 | def aconst_null(self, arguments, program): |
paul@181 | 1152 | program.load_const(None) |
paul@181 | 1153 | |
paul@181 | 1154 | def aload(self, arguments, program): |
paul@181 | 1155 | program.load_fast(arguments[0]) |
paul@181 | 1156 | |
paul@181 | 1157 | def aload_0(self, arguments, program): |
paul@181 | 1158 | program.load_fast(0) |
paul@181 | 1159 | |
paul@181 | 1160 | def aload_1(self, arguments, program): |
paul@181 | 1161 | program.load_fast(1) |
paul@181 | 1162 | |
paul@181 | 1163 | def aload_2(self, arguments, program): |
paul@181 | 1164 | program.load_fast(2) |
paul@181 | 1165 | |
paul@181 | 1166 | def aload_3(self, arguments, program): |
paul@181 | 1167 | program.load_fast(3) |
paul@181 | 1168 | |
paul@181 | 1169 | def anewarray(self, arguments, program): |
paul@181 | 1170 | # NOTE: Does not raise NegativeArraySizeException. |
paul@181 | 1171 | # NOTE: Not using the index to type the list/array. |
paul@181 | 1172 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1173 | type_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1174 | default_value = classfile.get_default_for_type(type_name) |
paul@181 | 1175 | self._newarray(program, type_name) |
paul@181 | 1176 | |
paul@181 | 1177 | def _newarray(self, program, default_value): |
paul@181 | 1178 | program.build_list(0) # Stack: count, list |
paul@181 | 1179 | program.rot_two() # Stack: list, count |
paul@181 | 1180 | program.setup_loop() |
paul@181 | 1181 | program.load_global("range") |
paul@181 | 1182 | program.load_const(0) # Stack: list, count, range, 0 |
paul@181 | 1183 | program.rot_three() # Stack: list, 0, count, range |
paul@181 | 1184 | program.rot_three() # Stack: list, range, 0, count |
paul@181 | 1185 | program.call_function(2) # Stack: list, range_list |
paul@181 | 1186 | program.get_iter() # Stack: list, iter |
paul@181 | 1187 | program.for_iter() # Stack: list, iter, value |
paul@181 | 1188 | program.pop_top() # Stack: list, iter |
paul@181 | 1189 | program.rot_two() # Stack: iter, list |
paul@181 | 1190 | program.dup_top() # Stack: iter, list, list |
paul@181 | 1191 | program.load_attr("append") # Stack: iter, list, append |
paul@181 | 1192 | program.load_const(default_value) # Stack: iter, list, append, default |
paul@181 | 1193 | program.call_function(1) # Stack: iter, list, default |
paul@181 | 1194 | program.pop_top() # Stack: iter, list |
paul@181 | 1195 | program.rot_two() # Stack: list, iter |
paul@181 | 1196 | program.end_loop() # Back to for_iter above |
paul@181 | 1197 | |
paul@181 | 1198 | def areturn(self, arguments, program): |
paul@181 | 1199 | program.return_value() |
paul@181 | 1200 | |
paul@181 | 1201 | def arraylength(self, arguments, program): |
paul@181 | 1202 | program.load_global("len") # Stack: arrayref, len |
paul@181 | 1203 | program.rot_two() # Stack: len, arrayref |
paul@181 | 1204 | program.call_function(1) |
paul@181 | 1205 | |
paul@181 | 1206 | def astore(self, arguments, program): |
paul@181 | 1207 | program.store_fast(arguments[0]) |
paul@181 | 1208 | |
paul@181 | 1209 | def astore_0(self, arguments, program): |
paul@181 | 1210 | program.store_fast(0) |
paul@181 | 1211 | |
paul@181 | 1212 | def astore_1(self, arguments, program): |
paul@181 | 1213 | program.store_fast(1) |
paul@181 | 1214 | |
paul@181 | 1215 | def astore_2(self, arguments, program): |
paul@181 | 1216 | program.store_fast(2) |
paul@181 | 1217 | |
paul@181 | 1218 | def astore_3(self, arguments, program): |
paul@181 | 1219 | program.store_fast(3) |
paul@181 | 1220 | |
paul@181 | 1221 | def athrow(self, arguments, program): |
paul@181 | 1222 | # NOTE: NullPointerException not raised where null/None is found on the stack. |
paul@181 | 1223 | # If this instruction appears in a finally handler, use end_finally instead. |
paul@181 | 1224 | if self.in_finally: |
paul@181 | 1225 | program.end_finally() |
paul@181 | 1226 | else: |
paul@181 | 1227 | # Wrap the exception in a Python exception. |
paul@181 | 1228 | program.load_global("Exception") # Stack: objectref, Exception |
paul@181 | 1229 | program.rot_two() # Stack: Exception, objectref |
paul@181 | 1230 | program.call_function(1) # Stack: exception |
paul@181 | 1231 | program.raise_varargs(1) |
paul@181 | 1232 | # NOTE: This seems to put another object on the stack. |
paul@181 | 1233 | |
paul@181 | 1234 | baload = aaload |
paul@181 | 1235 | bastore = aastore |
paul@181 | 1236 | |
paul@181 | 1237 | def bipush(self, arguments, program): |
paul@181 | 1238 | program.load_const(signed1(arguments[0])) |
paul@181 | 1239 | |
paul@181 | 1240 | caload = aaload |
paul@181 | 1241 | castore = aastore |
paul@181 | 1242 | |
paul@181 | 1243 | def checkcast(self, arguments, program): |
paul@181 | 1244 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1245 | target_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1246 | program.use_external_name(target_name) |
paul@181 | 1247 | program.dup_top() # Stack: objectref, objectref |
paul@181 | 1248 | program.load_const(None) # Stack: objectref, objectref, None |
paul@181 | 1249 | program.compare_op("is") # Stack: objectref, result |
paul@181 | 1250 | program.jump_to_label(1, "next") |
paul@181 | 1251 | program.pop_top() # Stack: objectref |
paul@181 | 1252 | program.dup_top() # Stack: objectref, objectref |
paul@181 | 1253 | program.load_global("isinstance") # Stack: objectref, objectref, isinstance |
paul@181 | 1254 | program.rot_two() # Stack: objectref, isinstance, objectref |
paul@181 | 1255 | load_class_name(self.class_file, target_name, program) |
paul@181 | 1256 | program.call_function(2) # Stack: objectref, result |
paul@181 | 1257 | program.jump_to_label(1, "next") |
paul@181 | 1258 | program.pop_top() # Stack: objectref |
paul@181 | 1259 | program.pop_top() # Stack: |
paul@181 | 1260 | program.use_external_name("java.lang.ClassCastException") |
paul@181 | 1261 | load_class_name(self.class_file, "java.lang.ClassCastException", program) |
paul@181 | 1262 | program.call_function(0) # Stack: exception |
paul@181 | 1263 | # Wrap the exception in a Python exception. |
paul@181 | 1264 | program.load_global("Exception") # Stack: exception, Exception |
paul@181 | 1265 | program.rot_two() # Stack: Exception, exception |
paul@181 | 1266 | program.call_function(1) # Stack: exception |
paul@181 | 1267 | program.raise_varargs(1) |
paul@181 | 1268 | # NOTE: This seems to put another object on the stack. |
paul@181 | 1269 | program.start_label("next") |
paul@181 | 1270 | program.pop_top() # Stack: objectref |
paul@181 | 1271 | |
paul@181 | 1272 | def d2f(self, arguments, program): |
paul@181 | 1273 | pass |
paul@181 | 1274 | |
paul@181 | 1275 | def d2i(self, arguments, program): |
paul@181 | 1276 | program.load_global("int") # Stack: value, int |
paul@181 | 1277 | program.rot_two() # Stack: int, value |
paul@181 | 1278 | program.call_function(1) # Stack: result |
paul@181 | 1279 | |
paul@181 | 1280 | d2l = d2i # Preserving Java semantics |
paul@181 | 1281 | |
paul@181 | 1282 | def dadd(self, arguments, program): |
paul@181 | 1283 | # NOTE: No type checking performed. |
paul@181 | 1284 | program.binary_add() |
paul@181 | 1285 | |
paul@181 | 1286 | daload = aaload |
paul@181 | 1287 | dastore = aastore |
paul@181 | 1288 | |
paul@181 | 1289 | def dcmpg(self, arguments, program): |
paul@181 | 1290 | # NOTE: No type checking performed. |
paul@181 | 1291 | program.compare_op(">") |
paul@181 | 1292 | |
paul@181 | 1293 | def dcmpl(self, arguments, program): |
paul@181 | 1294 | # NOTE: No type checking performed. |
paul@181 | 1295 | program.compare_op("<") |
paul@181 | 1296 | |
paul@181 | 1297 | def dconst_0(self, arguments, program): |
paul@181 | 1298 | program.load_const(0.0) |
paul@181 | 1299 | |
paul@181 | 1300 | def dconst_1(self, arguments, program): |
paul@181 | 1301 | program.load_const(1.0) |
paul@181 | 1302 | |
paul@181 | 1303 | def ddiv(self, arguments, program): |
paul@181 | 1304 | # NOTE: No type checking performed. |
paul@181 | 1305 | program.binary_divide() |
paul@181 | 1306 | |
paul@181 | 1307 | dload = aload |
paul@181 | 1308 | dload_0 = aload_0 |
paul@181 | 1309 | dload_1 = aload_1 |
paul@181 | 1310 | dload_2 = aload_2 |
paul@181 | 1311 | dload_3 = aload_3 |
paul@181 | 1312 | |
paul@181 | 1313 | def dmul(self, arguments, program): |
paul@181 | 1314 | # NOTE: No type checking performed. |
paul@181 | 1315 | program.binary_multiply() |
paul@181 | 1316 | |
paul@181 | 1317 | def dneg(self, arguments, program): |
paul@181 | 1318 | # NOTE: No type checking performed. |
paul@181 | 1319 | program.unary_negative() |
paul@181 | 1320 | |
paul@181 | 1321 | def drem(self, arguments, program): |
paul@181 | 1322 | # NOTE: No type checking performed. |
paul@181 | 1323 | program.binary_modulo() |
paul@181 | 1324 | |
paul@181 | 1325 | dreturn = areturn |
paul@181 | 1326 | dstore = astore |
paul@181 | 1327 | dstore_0 = astore_0 |
paul@181 | 1328 | dstore_1 = astore_1 |
paul@181 | 1329 | dstore_2 = astore_2 |
paul@181 | 1330 | dstore_3 = astore_3 |
paul@181 | 1331 | |
paul@181 | 1332 | def dsub(self, arguments, program): |
paul@181 | 1333 | # NOTE: No type checking performed. |
paul@181 | 1334 | program.binary_subtract() |
paul@181 | 1335 | |
paul@181 | 1336 | def dup(self, arguments, program): |
paul@181 | 1337 | program.dup_top() |
paul@181 | 1338 | |
paul@181 | 1339 | def dup_x1(self, arguments, program): |
paul@181 | 1340 | # Ignoring computational type categories. |
paul@181 | 1341 | program.dup_top() |
paul@181 | 1342 | program.rot_three() |
paul@181 | 1343 | |
paul@181 | 1344 | def dup_x2(self, arguments, program): |
paul@181 | 1345 | # Ignoring computational type categories. |
paul@181 | 1346 | program.dup_top() |
paul@181 | 1347 | program.rot_four() |
paul@181 | 1348 | |
paul@181 | 1349 | dup2 = dup # Ignoring computational type categories |
paul@181 | 1350 | dup2_x1 = dup_x1 # Ignoring computational type categories |
paul@181 | 1351 | dup2_x2 = dup_x2 # Ignoring computational type categories |
paul@181 | 1352 | |
paul@181 | 1353 | def f2d(self, arguments, program): |
paul@181 | 1354 | pass # Preserving Java semantics |
paul@181 | 1355 | |
paul@181 | 1356 | def f2i(self, arguments, program): |
paul@181 | 1357 | program.load_global("int") # Stack: value, int |
paul@181 | 1358 | program.rot_two() # Stack: int, value |
paul@181 | 1359 | program.call_function(1) # Stack: result |
paul@181 | 1360 | |
paul@181 | 1361 | f2l = f2i # Preserving Java semantics |
paul@181 | 1362 | fadd = dadd |
paul@181 | 1363 | faload = daload |
paul@181 | 1364 | fastore = dastore |
paul@181 | 1365 | fcmpg = dcmpg |
paul@181 | 1366 | fcmpl = dcmpl |
paul@181 | 1367 | fconst_0 = dconst_0 |
paul@181 | 1368 | fconst_1 = dconst_1 |
paul@181 | 1369 | |
paul@181 | 1370 | def fconst_2(self, arguments, program): |
paul@181 | 1371 | program.load_const(2.0) |
paul@181 | 1372 | |
paul@181 | 1373 | fdiv = ddiv |
paul@181 | 1374 | fload = dload |
paul@181 | 1375 | fload_0 = dload_0 |
paul@181 | 1376 | fload_1 = dload_1 |
paul@181 | 1377 | fload_2 = dload_2 |
paul@181 | 1378 | fload_3 = dload_3 |
paul@181 | 1379 | fmul = dmul |
paul@181 | 1380 | fneg = dneg |
paul@181 | 1381 | frem = drem |
paul@181 | 1382 | freturn = dreturn |
paul@181 | 1383 | fstore = dstore |
paul@181 | 1384 | fstore_0 = dstore_0 |
paul@181 | 1385 | fstore_1 = dstore_1 |
paul@181 | 1386 | fstore_2 = dstore_2 |
paul@181 | 1387 | fstore_3 = dstore_3 |
paul@181 | 1388 | fsub = dsub |
paul@181 | 1389 | |
paul@181 | 1390 | def getfield(self, arguments, program): |
paul@181 | 1391 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1392 | target_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1393 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@181 | 1394 | program.load_attr(str(target_name)) |
paul@181 | 1395 | |
paul@181 | 1396 | def getstatic(self, arguments, program): |
paul@181 | 1397 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1398 | target = self.class_file.constants[index - 1] |
paul@181 | 1399 | target_name = target.get_python_name() |
paul@181 | 1400 | |
paul@181 | 1401 | # Get the class name instead of the fully qualified name. |
paul@181 | 1402 | |
paul@181 | 1403 | full_class_name = target.get_class().get_python_name() |
paul@181 | 1404 | program.use_external_name(full_class_name) |
paul@181 | 1405 | load_class_name(self.class_file, full_class_name, program) |
paul@181 | 1406 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@181 | 1407 | program.load_attr(str(target_name)) |
paul@181 | 1408 | |
paul@181 | 1409 | def goto(self, arguments, program): |
paul@181 | 1410 | offset = signed2((arguments[0] << 8) + arguments[1]) |
paul@181 | 1411 | java_absolute = self.java_position + offset |
paul@181 | 1412 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1413 | |
paul@181 | 1414 | def goto_w(self, arguments, program): |
paul@181 | 1415 | offset = signed4((arguments[0] << 24) + (arguments[1] << 16) + (arguments[2] << 8) + arguments[3]) |
paul@181 | 1416 | java_absolute = self.java_position + offset |
paul@181 | 1417 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1418 | |
paul@181 | 1419 | def i2b(self, arguments, program): |
paul@181 | 1420 | pass |
paul@181 | 1421 | |
paul@181 | 1422 | def i2c(self, arguments, program): |
paul@181 | 1423 | pass |
paul@181 | 1424 | |
paul@181 | 1425 | def i2d(self, arguments, program): |
paul@181 | 1426 | program.load_global("float") # Stack: value, float |
paul@181 | 1427 | program.rot_two() # Stack: float, value |
paul@181 | 1428 | program.call_function(1) # Stack: result |
paul@181 | 1429 | |
paul@181 | 1430 | i2f = i2d # Not distinguishing between float and double |
paul@181 | 1431 | |
paul@181 | 1432 | def i2l(self, arguments, program): |
paul@181 | 1433 | pass # Preserving Java semantics |
paul@181 | 1434 | |
paul@181 | 1435 | def i2s(self, arguments, program): |
paul@181 | 1436 | pass # Not distinguishing between int and short |
paul@181 | 1437 | |
paul@181 | 1438 | iadd = fadd |
paul@181 | 1439 | iaload = faload |
paul@181 | 1440 | |
paul@181 | 1441 | def iand(self, arguments, program): |
paul@181 | 1442 | # NOTE: No type checking performed. |
paul@181 | 1443 | program.binary_and() |
paul@181 | 1444 | |
paul@181 | 1445 | iastore = fastore |
paul@181 | 1446 | |
paul@181 | 1447 | def iconst_m1(self, arguments, program): |
paul@181 | 1448 | program.load_const(-1) |
paul@181 | 1449 | |
paul@181 | 1450 | def iconst_0(self, arguments, program): |
paul@181 | 1451 | program.load_const(0) |
paul@181 | 1452 | |
paul@181 | 1453 | def iconst_1(self, arguments, program): |
paul@181 | 1454 | program.load_const(1) |
paul@181 | 1455 | |
paul@181 | 1456 | def iconst_2(self, arguments, program): |
paul@181 | 1457 | program.load_const(2) |
paul@181 | 1458 | |
paul@181 | 1459 | def iconst_3(self, arguments, program): |
paul@181 | 1460 | program.load_const(3) |
paul@181 | 1461 | |
paul@181 | 1462 | def iconst_4(self, arguments, program): |
paul@181 | 1463 | program.load_const(4) |
paul@181 | 1464 | |
paul@181 | 1465 | def iconst_5(self, arguments, program): |
paul@181 | 1466 | program.load_const(5) |
paul@181 | 1467 | |
paul@181 | 1468 | idiv = fdiv |
paul@181 | 1469 | |
paul@181 | 1470 | def _if_xcmpx(self, arguments, program, op): |
paul@181 | 1471 | offset = signed2((arguments[0] << 8) + arguments[1]) |
paul@181 | 1472 | java_absolute = self.java_position + offset |
paul@181 | 1473 | program.compare_op(op) |
paul@181 | 1474 | program.jump_to_label(0, "next") # skip if false |
paul@181 | 1475 | program.pop_top() |
paul@181 | 1476 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1477 | program.start_label("next") |
paul@181 | 1478 | program.pop_top() |
paul@181 | 1479 | |
paul@181 | 1480 | def if_acmpeq(self, arguments, program): |
paul@181 | 1481 | # NOTE: No type checking performed. |
paul@181 | 1482 | self._if_xcmpx(arguments, program, "is") |
paul@181 | 1483 | |
paul@181 | 1484 | def if_acmpne(self, arguments, program): |
paul@181 | 1485 | # NOTE: No type checking performed. |
paul@181 | 1486 | self._if_xcmpx(arguments, program, "is not") |
paul@181 | 1487 | |
paul@181 | 1488 | def if_icmpeq(self, arguments, program): |
paul@181 | 1489 | # NOTE: No type checking performed. |
paul@181 | 1490 | self._if_xcmpx(arguments, program, "==") |
paul@181 | 1491 | |
paul@181 | 1492 | def if_icmpne(self, arguments, program): |
paul@181 | 1493 | # NOTE: No type checking performed. |
paul@181 | 1494 | self._if_xcmpx(arguments, program, "!=") |
paul@181 | 1495 | |
paul@181 | 1496 | def if_icmplt(self, arguments, program): |
paul@181 | 1497 | # NOTE: No type checking performed. |
paul@181 | 1498 | self._if_xcmpx(arguments, program, "<") |
paul@181 | 1499 | |
paul@181 | 1500 | def if_icmpge(self, arguments, program): |
paul@181 | 1501 | # NOTE: No type checking performed. |
paul@181 | 1502 | self._if_xcmpx(arguments, program, ">=") |
paul@181 | 1503 | |
paul@181 | 1504 | def if_icmpgt(self, arguments, program): |
paul@181 | 1505 | # NOTE: No type checking performed. |
paul@181 | 1506 | self._if_xcmpx(arguments, program, ">") |
paul@181 | 1507 | |
paul@181 | 1508 | def if_icmple(self, arguments, program): |
paul@181 | 1509 | # NOTE: No type checking performed. |
paul@181 | 1510 | self._if_xcmpx(arguments, program, "<=") |
paul@181 | 1511 | |
paul@181 | 1512 | def ifeq(self, arguments, program): |
paul@181 | 1513 | # NOTE: No type checking performed. |
paul@181 | 1514 | program.load_const(0) |
paul@181 | 1515 | self._if_xcmpx(arguments, program, "==") |
paul@181 | 1516 | |
paul@181 | 1517 | def ifne(self, arguments, program): |
paul@181 | 1518 | # NOTE: No type checking performed. |
paul@181 | 1519 | program.load_const(0) |
paul@181 | 1520 | self._if_xcmpx(arguments, program, "!=") |
paul@181 | 1521 | |
paul@181 | 1522 | def iflt(self, arguments, program): |
paul@181 | 1523 | # NOTE: No type checking performed. |
paul@181 | 1524 | program.load_const(0) |
paul@181 | 1525 | self._if_xcmpx(arguments, program, "<") |
paul@181 | 1526 | |
paul@181 | 1527 | def ifge(self, arguments, program): |
paul@181 | 1528 | # NOTE: No type checking performed. |
paul@181 | 1529 | program.load_const(0) |
paul@181 | 1530 | self._if_xcmpx(arguments, program, ">=") |
paul@181 | 1531 | |
paul@181 | 1532 | def ifgt(self, arguments, program): |
paul@181 | 1533 | # NOTE: No type checking performed. |
paul@181 | 1534 | program.load_const(0) |
paul@181 | 1535 | self._if_xcmpx(arguments, program, ">") |
paul@181 | 1536 | |
paul@181 | 1537 | def ifle(self, arguments, program): |
paul@181 | 1538 | # NOTE: No type checking performed. |
paul@181 | 1539 | program.load_const(0) |
paul@181 | 1540 | self._if_xcmpx(arguments, program, "<=") |
paul@181 | 1541 | |
paul@181 | 1542 | def ifnonnull(self, arguments, program): |
paul@181 | 1543 | # NOTE: No type checking performed. |
paul@181 | 1544 | program.load_const(None) |
paul@181 | 1545 | self._if_xcmpx(arguments, program, "is not") |
paul@181 | 1546 | |
paul@181 | 1547 | def ifnull(self, arguments, program): |
paul@181 | 1548 | # NOTE: No type checking performed. |
paul@181 | 1549 | program.load_const(None) |
paul@181 | 1550 | self._if_xcmpx(arguments, program, "is") |
paul@181 | 1551 | |
paul@181 | 1552 | def iinc(self, arguments, program): |
paul@181 | 1553 | # NOTE: No type checking performed. |
paul@181 | 1554 | program.load_fast(arguments[0]) |
paul@181 | 1555 | program.load_const(arguments[1]) |
paul@181 | 1556 | program.binary_add() |
paul@181 | 1557 | program.store_fast(arguments[0]) |
paul@181 | 1558 | |
paul@181 | 1559 | iload = fload |
paul@181 | 1560 | iload_0 = fload_0 |
paul@181 | 1561 | iload_1 = fload_1 |
paul@181 | 1562 | iload_2 = fload_2 |
paul@181 | 1563 | iload_3 = fload_3 |
paul@181 | 1564 | imul = fmul |
paul@181 | 1565 | ineg = fneg |
paul@181 | 1566 | |
paul@181 | 1567 | def instanceof(self, arguments, program): |
paul@181 | 1568 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1569 | target_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1570 | program.use_external_name(target_name) |
paul@181 | 1571 | program.load_global("isinstance") # Stack: objectref, isinstance |
paul@181 | 1572 | program.rot_two() # Stack: isinstance, objectref |
paul@181 | 1573 | load_class_name(self.class_file, target_name, program) |
paul@181 | 1574 | program.call_function(2) # Stack: result |
paul@181 | 1575 | |
paul@181 | 1576 | def _invoke(self, target_name, program): |
paul@181 | 1577 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@181 | 1578 | program.load_attr(str(target_name)) # Stack: tuple, method |
paul@181 | 1579 | program.rot_two() # Stack: method, tuple |
paul@181 | 1580 | program.call_function_var(0) # Stack: result |
paul@181 | 1581 | |
paul@181 | 1582 | def invokeinterface(self, arguments, program): |
paul@181 | 1583 | # NOTE: This implementation does not perform the necessary checks for |
paul@181 | 1584 | # NOTE: signature-based polymorphism. |
paul@181 | 1585 | # NOTE: Java rules not specifically obeyed. |
paul@181 | 1586 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1587 | # NOTE: "count" == nargs + 1, apparently. |
paul@181 | 1588 | count = arguments[2] - 1 |
paul@181 | 1589 | target_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1590 | # Stack: objectref, arg1, arg2, ... |
paul@181 | 1591 | program.build_tuple(count) # Stack: objectref, tuple |
paul@181 | 1592 | program.rot_two() # Stack: tuple, objectref |
paul@181 | 1593 | # NOTE: The interface information is not used to discover the correct |
paul@181 | 1594 | # NOTE: method. |
paul@181 | 1595 | self._invoke(target_name, program) |
paul@181 | 1596 | |
paul@181 | 1597 | def invokespecial(self, arguments, program): |
paul@181 | 1598 | # NOTE: This implementation does not perform the necessary checks for |
paul@181 | 1599 | # NOTE: signature-based polymorphism. |
paul@181 | 1600 | # NOTE: Java rules not specifically obeyed. |
paul@181 | 1601 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1602 | target = self.class_file.constants[index - 1] |
paul@181 | 1603 | original_name = target.get_name() |
paul@181 | 1604 | target_name = target.get_python_name() |
paul@181 | 1605 | |
paul@181 | 1606 | # Get the number of parameters from the descriptor. |
paul@181 | 1607 | |
paul@181 | 1608 | count = len(target.get_descriptor()[0]) |
paul@181 | 1609 | |
paul@181 | 1610 | # First, we build a tuple of the reference and arguments. |
paul@181 | 1611 | |
paul@181 | 1612 | program.build_tuple(count + 1) # Stack: tuple |
paul@181 | 1613 | |
paul@181 | 1614 | # Get the class name instead of the fully qualified name. |
paul@181 | 1615 | # NOTE: Not bothering with Object initialisation. |
paul@181 | 1616 | |
paul@181 | 1617 | full_class_name = target.get_class().get_python_name() |
paul@181 | 1618 | if full_class_name not in ("java.lang.Object", "java.lang.Exception"): |
paul@181 | 1619 | program.use_external_name(full_class_name) |
paul@181 | 1620 | load_class_name(self.class_file, full_class_name, program) |
paul@181 | 1621 | self._invoke(target_name, program) |
paul@181 | 1622 | |
paul@181 | 1623 | # Remove Python None return value. |
paul@181 | 1624 | |
paul@181 | 1625 | if str(original_name) == "<init>": |
paul@181 | 1626 | program.pop_top() |
paul@181 | 1627 | |
paul@181 | 1628 | def invokestatic(self, arguments, program): |
paul@181 | 1629 | # NOTE: This implementation does not perform the necessary checks for |
paul@181 | 1630 | # NOTE: signature-based polymorphism. |
paul@181 | 1631 | # NOTE: Java rules not specifically obeyed. |
paul@181 | 1632 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1633 | target = self.class_file.constants[index - 1] |
paul@181 | 1634 | target_name = target.get_python_name() |
paul@181 | 1635 | |
paul@181 | 1636 | # Get the number of parameters from the descriptor. |
paul@181 | 1637 | |
paul@181 | 1638 | count = len(target.get_descriptor()[0]) |
paul@181 | 1639 | |
paul@181 | 1640 | # Stack: arg1, arg2, ... |
paul@181 | 1641 | |
paul@181 | 1642 | program.build_tuple(count) # Stack: tuple |
paul@181 | 1643 | |
paul@181 | 1644 | # Use the class to provide access to static methods. |
paul@181 | 1645 | # Get the class name instead of the fully qualified name. |
paul@181 | 1646 | |
paul@181 | 1647 | full_class_name = target.get_class().get_python_name() |
paul@181 | 1648 | if full_class_name not in ("java.lang.Object", "java.lang.Exception"): |
paul@181 | 1649 | program.use_external_name(full_class_name) |
paul@181 | 1650 | load_class_name(self.class_file, full_class_name, program) |
paul@181 | 1651 | self._invoke(target_name, program) |
paul@181 | 1652 | |
paul@181 | 1653 | def invokevirtual (self, arguments, program): |
paul@181 | 1654 | # NOTE: This implementation does not perform the necessary checks for |
paul@181 | 1655 | # NOTE: signature-based polymorphism. |
paul@181 | 1656 | # NOTE: Java rules not specifically obeyed. |
paul@181 | 1657 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1658 | target = self.class_file.constants[index - 1] |
paul@181 | 1659 | target_name = target.get_python_name() |
paul@181 | 1660 | # Get the number of parameters from the descriptor. |
paul@181 | 1661 | count = len(target.get_descriptor()[0]) |
paul@181 | 1662 | # Stack: objectref, arg1, arg2, ... |
paul@181 | 1663 | program.build_tuple(count) # Stack: objectref, tuple |
paul@181 | 1664 | program.rot_two() # Stack: tuple, objectref |
paul@181 | 1665 | self._invoke(target_name, program) |
paul@181 | 1666 | |
paul@181 | 1667 | def ior(self, arguments, program): |
paul@181 | 1668 | # NOTE: No type checking performed. |
paul@181 | 1669 | program.binary_or() |
paul@181 | 1670 | |
paul@181 | 1671 | irem = frem |
paul@181 | 1672 | ireturn = freturn |
paul@181 | 1673 | |
paul@181 | 1674 | def ishl(self, arguments, program): |
paul@181 | 1675 | # NOTE: No type checking performed. |
paul@181 | 1676 | # NOTE: Not verified. |
paul@181 | 1677 | program.binary_lshift() |
paul@181 | 1678 | |
paul@181 | 1679 | def ishr(self, arguments, program): |
paul@181 | 1680 | # NOTE: No type checking performed. |
paul@181 | 1681 | # NOTE: Not verified. |
paul@181 | 1682 | program.binary_rshift() |
paul@181 | 1683 | |
paul@181 | 1684 | istore = fstore |
paul@181 | 1685 | istore_0 = fstore_0 |
paul@181 | 1686 | istore_1 = fstore_1 |
paul@181 | 1687 | istore_2 = fstore_2 |
paul@181 | 1688 | istore_3 = fstore_3 |
paul@181 | 1689 | isub = fsub |
paul@181 | 1690 | iushr = ishr # Ignoring distinctions between arithmetic and logical shifts |
paul@181 | 1691 | |
paul@181 | 1692 | def ixor(self, arguments, program): |
paul@181 | 1693 | # NOTE: No type checking performed. |
paul@181 | 1694 | program.binary_xor() |
paul@181 | 1695 | |
paul@181 | 1696 | def jsr(self, arguments, program): |
paul@181 | 1697 | offset = signed2((arguments[0] << 8) + arguments[1]) |
paul@181 | 1698 | java_absolute = self.java_position + offset |
paul@181 | 1699 | # Store the address of the next instruction. |
paul@181 | 1700 | program.load_const_ret(self.position_mapping[self.java_position + 3]) |
paul@181 | 1701 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1702 | |
paul@181 | 1703 | def jsr_w(self, arguments, program): |
paul@181 | 1704 | offset = signed4((arguments[0] << 24) + (arguments[1] << 16) + (arguments[2] << 8) + arguments[3]) |
paul@181 | 1705 | java_absolute = self.java_position + offset |
paul@181 | 1706 | # Store the address of the next instruction. |
paul@181 | 1707 | program.load_const_ret(self.position_mapping[self.java_position + 5]) |
paul@181 | 1708 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1709 | |
paul@181 | 1710 | l2d = i2d |
paul@181 | 1711 | l2f = i2f |
paul@181 | 1712 | |
paul@181 | 1713 | def l2i(self, arguments, program): |
paul@181 | 1714 | pass # Preserving Java semantics |
paul@181 | 1715 | |
paul@181 | 1716 | ladd = iadd |
paul@181 | 1717 | laload = iaload |
paul@181 | 1718 | land = iand |
paul@181 | 1719 | lastore = iastore |
paul@181 | 1720 | |
paul@181 | 1721 | def lcmp(self, arguments, program): |
paul@181 | 1722 | # NOTE: No type checking performed. |
paul@181 | 1723 | program.dup_topx(2) # Stack: value1, value2, value1, value2 |
paul@181 | 1724 | program.compare_op(">") # Stack: value1, value2, result |
paul@181 | 1725 | program.jump_to_label(0, "equals") |
paul@181 | 1726 | # True - produce result and branch. |
paul@181 | 1727 | program.pop_top() # Stack: value1, value2 |
paul@181 | 1728 | program.pop_top() # Stack: value1 |
paul@181 | 1729 | program.pop_top() # Stack: |
paul@181 | 1730 | program.load_const(1) # Stack: 1 |
paul@181 | 1731 | program.jump_to_label(None, "next") |
paul@181 | 1732 | # False - test equality. |
paul@181 | 1733 | program.start_label("equals") |
paul@181 | 1734 | program.pop_top() # Stack: value1, value2 |
paul@181 | 1735 | program.dup_topx(2) # Stack: value1, value2, value1, value2 |
paul@181 | 1736 | program.compare_op("==") # Stack: value1, value2, result |
paul@181 | 1737 | program.jump_to_label(0, "less") |
paul@181 | 1738 | # True - produce result and branch. |
paul@181 | 1739 | program.pop_top() # Stack: value1, value2 |
paul@181 | 1740 | program.pop_top() # Stack: value1 |
paul@181 | 1741 | program.pop_top() # Stack: |
paul@181 | 1742 | program.load_const(0) # Stack: 0 |
paul@181 | 1743 | program.jump_to_label(None, "next") |
paul@181 | 1744 | # False - produce result. |
paul@181 | 1745 | program.start_label("less") |
paul@181 | 1746 | program.pop_top() # Stack: value1, value2 |
paul@181 | 1747 | program.pop_top() # Stack: value1 |
paul@181 | 1748 | program.pop_top() # Stack: |
paul@181 | 1749 | program.load_const(-1) # Stack: -1 |
paul@181 | 1750 | program.start_label("next") |
paul@181 | 1751 | |
paul@181 | 1752 | lconst_0 = iconst_0 |
paul@181 | 1753 | lconst_1 = iconst_1 |
paul@181 | 1754 | |
paul@181 | 1755 | def ldc(self, arguments, program): |
paul@181 | 1756 | const = self.class_file.constants[arguments[0] - 1] |
paul@181 | 1757 | if isinstance(const, classfile.StringInfo): |
paul@181 | 1758 | program.use_external_name("java.lang.String") |
paul@181 | 1759 | program.load_global("java") |
paul@181 | 1760 | program.load_attr("lang") |
paul@181 | 1761 | program.load_attr("String") |
paul@181 | 1762 | program.load_const(const.get_value()) |
paul@181 | 1763 | program.call_function(1) |
paul@181 | 1764 | else: |
paul@181 | 1765 | program.load_const(const.get_value()) |
paul@181 | 1766 | |
paul@181 | 1767 | def ldc_w(self, arguments, program): |
paul@181 | 1768 | const = self.class_file.constants[(arguments[0] << 8) + arguments[1] - 1] |
paul@181 | 1769 | if isinstance(const, classfile.StringInfo): |
paul@181 | 1770 | program.use_external_name("java.lang.String") |
paul@181 | 1771 | program.load_global("java") |
paul@181 | 1772 | program.load_attr("lang") |
paul@181 | 1773 | program.load_attr("String") |
paul@181 | 1774 | program.load_const(const.get_value()) |
paul@181 | 1775 | program.call_function(1) |
paul@181 | 1776 | else: |
paul@181 | 1777 | program.load_const(const.get_value()) |
paul@181 | 1778 | |
paul@181 | 1779 | ldc2_w = ldc_w |
paul@181 | 1780 | ldiv = idiv |
paul@181 | 1781 | lload = iload |
paul@181 | 1782 | lload_0 = iload_0 |
paul@181 | 1783 | lload_1 = iload_1 |
paul@181 | 1784 | lload_2 = iload_2 |
paul@181 | 1785 | lload_3 = iload_3 |
paul@181 | 1786 | lmul = imul |
paul@181 | 1787 | lneg = ineg |
paul@181 | 1788 | |
paul@181 | 1789 | def lookupswitch(self, code, program): |
paul@181 | 1790 | |
paul@181 | 1791 | # Find the offset to the next 4 byte boundary in the code. |
paul@181 | 1792 | |
paul@181 | 1793 | d, r = divmod(self.java_position + 1, 4) |
paul@181 | 1794 | to_boundary = (4 - r) % 4 |
paul@181 | 1795 | |
paul@181 | 1796 | # Get the pertinent arguments. |
paul@181 | 1797 | |
paul@181 | 1798 | code = code[to_boundary:] |
paul@181 | 1799 | default = classfile.s4(code[0:4]) |
paul@181 | 1800 | npairs = classfile.s4(code[4:8]) |
paul@181 | 1801 | |
paul@181 | 1802 | # Process the pairs. |
paul@181 | 1803 | # NOTE: This is not the most optimal implementation. |
paul@181 | 1804 | |
paul@181 | 1805 | pair_index = 8 |
paul@181 | 1806 | for pair in range(0, npairs): |
paul@181 | 1807 | match = classfile.u4(code[pair_index:pair_index+4]) |
paul@181 | 1808 | offset = classfile.s4(code[pair_index+4:pair_index+8]) |
paul@181 | 1809 | # Calculate the branch target. |
paul@181 | 1810 | java_absolute = self.java_position + offset |
paul@181 | 1811 | # Generate branching code. |
paul@181 | 1812 | program.dup_top() # Stack: key, key |
paul@181 | 1813 | program.load_const(match) # Stack: key, key, match |
paul@181 | 1814 | program.compare_op("==") # Stack: key, result |
paul@181 | 1815 | program.jump_to_label(0, "end") |
paul@181 | 1816 | program.pop_top() # Stack: key |
paul@181 | 1817 | program.pop_top() # Stack: |
paul@181 | 1818 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1819 | # Generate the label for the end of the branching code. |
paul@181 | 1820 | program.start_label("end") |
paul@181 | 1821 | program.pop_top() # Stack: key |
paul@181 | 1822 | # Update the index. |
paul@181 | 1823 | pair_index += 8 |
paul@181 | 1824 | |
paul@181 | 1825 | # Generate the default. |
paul@181 | 1826 | |
paul@181 | 1827 | java_absolute = self.java_position + default |
paul@181 | 1828 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1829 | return pair_index + to_boundary |
paul@181 | 1830 | |
paul@181 | 1831 | lor = ior |
paul@181 | 1832 | lrem = irem |
paul@181 | 1833 | lreturn = ireturn |
paul@181 | 1834 | lshl = ishl |
paul@181 | 1835 | lshr = ishr |
paul@181 | 1836 | lstore = istore |
paul@181 | 1837 | lstore_0 = istore_0 |
paul@181 | 1838 | lstore_1 = istore_1 |
paul@181 | 1839 | lstore_2 = istore_2 |
paul@181 | 1840 | lstore_3 = istore_3 |
paul@181 | 1841 | lsub = isub |
paul@181 | 1842 | lushr = iushr |
paul@181 | 1843 | lxor = ixor |
paul@181 | 1844 | |
paul@181 | 1845 | def monitorenter(self, arguments, program): |
paul@181 | 1846 | # NOTE: To be implemented. |
paul@181 | 1847 | pass |
paul@181 | 1848 | |
paul@181 | 1849 | def monitorexit(self, arguments, program): |
paul@181 | 1850 | # NOTE: To be implemented. |
paul@181 | 1851 | pass |
paul@181 | 1852 | |
paul@181 | 1853 | def multianewarray(self, arguments, program): |
paul@181 | 1854 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1855 | dimensions = arguments[2] |
paul@181 | 1856 | # Stack: count1, ..., countN-1, countN |
paul@181 | 1857 | type_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1858 | default_value = classfile.get_default_for_type(type_name) |
paul@181 | 1859 | self._newarray(program, default_value) # Stack: count1, ..., countN-1, list |
paul@181 | 1860 | for dimension in range(1, dimensions): |
paul@181 | 1861 | program.rot_two() # Stack: count1, ..., list, countN-1 |
paul@181 | 1862 | program.build_list(0) # Stack: count1, ..., list, countN-1, new-list |
paul@181 | 1863 | program.rot_three() # Stack: count1, ..., new-list, list, countN-1 |
paul@181 | 1864 | program.setup_loop() |
paul@181 | 1865 | program.load_const(0) # Stack: count1, ..., new-list, list, countN-1, 0 |
paul@181 | 1866 | program.rot_two() # Stack: count1, ..., new-list, list, 0, countN-1 |
paul@181 | 1867 | program.load_global("range") # Stack: count1, ..., new-list, list, 0, countN-1, range |
paul@181 | 1868 | program.rot_three() # Stack: count1, ..., new-list, list, range, 0, countN-1 |
paul@181 | 1869 | program.call_function(2) # Stack: count1, ..., new-list, list, range-list |
paul@181 | 1870 | program.get_iter() # Stack: count1, ..., new-list, list, iter |
paul@181 | 1871 | program.for_iter() # Stack: count1, ..., new-list, list, iter, value |
paul@181 | 1872 | program.pop_top() # Stack: count1, ..., new-list, list, iter |
paul@181 | 1873 | program.rot_three() # Stack: count1, ..., iter, new-list, list |
paul@181 | 1874 | program.slice_0() # Stack: count1, ..., iter, new-list, list[:] |
paul@181 | 1875 | program.dup_top() # Stack: count1, ..., iter, new-list, list[:], list[:] |
paul@181 | 1876 | program.rot_three() # Stack: count1, ..., iter, list[:], new-list, list[:] |
paul@181 | 1877 | program.rot_two() # Stack: count1, ..., iter, list[:], list[:], new-list |
paul@181 | 1878 | program.dup_top() # Stack: count1, ..., iter, list[:], list[:], new-list, new-list |
paul@181 | 1879 | program.load_attr("append") # Stack: count1, ..., iter, list[:], list[:], new-list, append |
paul@181 | 1880 | program.rot_three() # Stack: count1, ..., iter, list[:], append, list[:], new-list |
paul@181 | 1881 | program.rot_three() # Stack: count1, ..., iter, list[:], new-list, append, list[:] |
paul@181 | 1882 | program.call_function(1) # Stack: count1, ..., iter, list[:], new-list, None |
paul@181 | 1883 | program.pop_top() # Stack: count1, ..., iter, list[:], new-list |
paul@181 | 1884 | program.rot_two() # Stack: count1, ..., iter, new-list, list[:] |
paul@181 | 1885 | program.rot_three() # Stack: count1, ..., list[:], iter, new-list |
paul@181 | 1886 | program.rot_three() # Stack: count1, ..., new-list, list[:], iter |
paul@181 | 1887 | program.end_loop() # Stack: count1, ..., new-list, list[:], iter |
paul@181 | 1888 | program.pop_top() # Stack: count1, ..., new-list |
paul@181 | 1889 | |
paul@181 | 1890 | def new(self, arguments, program): |
paul@181 | 1891 | # This operation is considered to be the same as the calling of the |
paul@181 | 1892 | # initialisation method of the given class with no arguments. |
paul@181 | 1893 | |
paul@181 | 1894 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1895 | target_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1896 | program.use_external_name(target_name) |
paul@181 | 1897 | |
paul@181 | 1898 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@181 | 1899 | program.load_global("object") |
paul@181 | 1900 | program.load_attr("__new__") |
paul@181 | 1901 | load_class_name(self.class_file, target_name, program) |
paul@181 | 1902 | program.call_function(1) |
paul@181 | 1903 | |
paul@181 | 1904 | def newarray(self, arguments, program): |
paul@181 | 1905 | # NOTE: Does not raise NegativeArraySizeException. |
paul@181 | 1906 | # NOTE: Not completely using the arguments to type the list/array. |
paul@181 | 1907 | atype = arguments[0] |
paul@181 | 1908 | default_value = get_default_for_atype(atype) |
paul@181 | 1909 | self._newarray(program, default_value) |
paul@181 | 1910 | |
paul@181 | 1911 | def nop(self, arguments, program): |
paul@181 | 1912 | pass |
paul@181 | 1913 | |
paul@181 | 1914 | def pop(self, arguments, program): |
paul@181 | 1915 | program.pop_top() |
paul@181 | 1916 | |
paul@181 | 1917 | pop2 = pop # ignoring Java stack value distinctions |
paul@181 | 1918 | |
paul@181 | 1919 | def putfield(self, arguments, program): |
paul@181 | 1920 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1921 | target_name = self.class_file.constants[index - 1].get_python_name() |
paul@181 | 1922 | program.rot_two() |
paul@181 | 1923 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@181 | 1924 | program.store_attr(str(target_name)) |
paul@181 | 1925 | |
paul@181 | 1926 | def putstatic(self, arguments, program): |
paul@181 | 1927 | index = (arguments[0] << 8) + arguments[1] |
paul@181 | 1928 | target = self.class_file.constants[index - 1] |
paul@181 | 1929 | target_name = target.get_python_name() |
paul@181 | 1930 | |
paul@181 | 1931 | # Get the class name instead of the fully qualified name. |
paul@181 | 1932 | |
paul@181 | 1933 | full_class_name = target.get_class().get_python_name() |
paul@181 | 1934 | program.use_external_name(full_class_name) |
paul@181 | 1935 | load_class_name(self.class_file, full_class_name, program) |
paul@181 | 1936 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@181 | 1937 | program.store_attr(str(target_name)) |
paul@181 | 1938 | |
paul@181 | 1939 | def ret(self, arguments, program): |
paul@181 | 1940 | program.ret(arguments[0]) |
paul@181 | 1941 | # Indicate that the finally handler is probably over. |
paul@181 | 1942 | # NOTE: This is seemingly not guaranteed. |
paul@181 | 1943 | self.in_finally = 0 |
paul@181 | 1944 | |
paul@181 | 1945 | def return_(self, arguments, program): |
paul@181 | 1946 | program.load_const(None) |
paul@181 | 1947 | program.return_value() |
paul@181 | 1948 | |
paul@181 | 1949 | saload = laload |
paul@181 | 1950 | sastore = lastore |
paul@181 | 1951 | |
paul@181 | 1952 | def sipush(self, arguments, program): |
paul@181 | 1953 | program.load_const(signed2((arguments[0] << 8) + arguments[1])) |
paul@181 | 1954 | |
paul@181 | 1955 | def swap(self, arguments, program): |
paul@181 | 1956 | program.rot_two() |
paul@181 | 1957 | |
paul@181 | 1958 | def tableswitch(self, code, program): |
paul@181 | 1959 | |
paul@181 | 1960 | # Find the offset to the next 4 byte boundary in the code. |
paul@181 | 1961 | |
paul@181 | 1962 | d, r = divmod(self.java_position + 1, 4) |
paul@181 | 1963 | to_boundary = (4 - r) % 4 |
paul@181 | 1964 | |
paul@181 | 1965 | # Get the pertinent arguments. |
paul@181 | 1966 | |
paul@181 | 1967 | code = code[to_boundary:] |
paul@181 | 1968 | default = classfile.s4(code[0:4]) |
paul@181 | 1969 | low = classfile.s4(code[4:8]) |
paul@181 | 1970 | high = classfile.s4(code[8:12]) |
paul@181 | 1971 | |
paul@181 | 1972 | # Process the jump entries. |
paul@181 | 1973 | # NOTE: This is not the most optimal implementation. |
paul@181 | 1974 | |
paul@181 | 1975 | jump_index = 12 |
paul@181 | 1976 | for jump in range(low, high + 1): |
paul@181 | 1977 | offset = classfile.s4(code[jump_index:jump_index + 4]) |
paul@181 | 1978 | |
paul@181 | 1979 | # Calculate the branch target. |
paul@181 | 1980 | |
paul@181 | 1981 | java_absolute = self.java_position + offset |
paul@181 | 1982 | |
paul@181 | 1983 | # Generate branching code. |
paul@181 | 1984 | |
paul@181 | 1985 | program.dup_top() # Stack: key, key |
paul@181 | 1986 | program.load_const(jump) # Stack: key, key, jump |
paul@181 | 1987 | program.compare_op("==") # Stack: key, result |
paul@181 | 1988 | program.jump_to_label(0, "end") |
paul@181 | 1989 | program.pop_top() # Stack: key |
paul@181 | 1990 | program.pop_top() # Stack: |
paul@181 | 1991 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 1992 | |
paul@181 | 1993 | # Generate the label for the end of the branching code. |
paul@181 | 1994 | |
paul@181 | 1995 | program.start_label("end") |
paul@181 | 1996 | program.pop_top() # Stack: key |
paul@181 | 1997 | |
paul@181 | 1998 | # Update the index. |
paul@181 | 1999 | |
paul@181 | 2000 | jump_index += 4 |
paul@181 | 2001 | |
paul@181 | 2002 | # Generate the default. |
paul@181 | 2003 | |
paul@181 | 2004 | java_absolute = self.java_position + default |
paul@181 | 2005 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@181 | 2006 | return jump_index + to_boundary |
paul@181 | 2007 | |
paul@181 | 2008 | def wide(self, code, program): |
paul@181 | 2009 | # NOTE: To be implemented. |
paul@181 | 2010 | raise NotImplementedError, "wide" |
paul@181 | 2011 | |
paul@181 | 2012 | def disassemble(class_file, method): |
paul@181 | 2013 | disassembler = BytecodeDisassembler(class_file) |
paul@181 | 2014 | disassembler.process(method, BytecodeDisassemblerProgram()) |
paul@181 | 2015 | return disassembler |
paul@181 | 2016 | |
paul@181 | 2017 | class ClassTranslator: |
paul@181 | 2018 | |
paul@181 | 2019 | """ |
paul@181 | 2020 | A class which provides a wrapper around a class file and the means to |
paul@181 | 2021 | translate the represented class into a Python class. |
paul@181 | 2022 | """ |
paul@181 | 2023 | |
paul@181 | 2024 | def __init__(self, class_file): |
paul@181 | 2025 | |
paul@181 | 2026 | "Initialise the object with the given 'class_file'." |
paul@181 | 2027 | |
paul@181 | 2028 | self.class_file = class_file |
paul@181 | 2029 | self.filename = "" |
paul@181 | 2030 | |
paul@181 | 2031 | for attribute in self.class_file.attributes: |
paul@181 | 2032 | if isinstance(attribute, classfile.SourceFileAttributeInfo): |
paul@181 | 2033 | self.filename = str(attribute.get_name()) |
paul@181 | 2034 | |
paul@181 | 2035 | def translate_method(self, method): |
paul@181 | 2036 | |
paul@181 | 2037 | "Translate the given 'method' - an object obtained from the class file." |
paul@181 | 2038 | |
paul@181 | 2039 | translator = BytecodeTranslator(self.class_file) |
paul@181 | 2040 | writer = BytecodeWriter() |
paul@181 | 2041 | try: |
paul@181 | 2042 | translator.process(method, writer) |
paul@181 | 2043 | except: |
paul@181 | 2044 | print "Translation error in", str(self.class_file.this_class.get_name()), str(method.get_name()) |
paul@181 | 2045 | disassemble(self.class_file, method) |
paul@181 | 2046 | raise |
paul@181 | 2047 | return translator, writer |
paul@181 | 2048 | |
paul@181 | 2049 | def make_method(self, real_method_name, methods, global_names): |
paul@181 | 2050 | |
paul@181 | 2051 | """ |
paul@181 | 2052 | Make a dispatcher method with the given 'real_method_name', providing |
paul@181 | 2053 | dispatch to the supplied type-sensitive 'methods', accessing the given |
paul@181 | 2054 | 'global_names' where necessary, and storing the new method in the |
paul@181 | 2055 | class's namespace. |
paul@181 | 2056 | """ |
paul@181 | 2057 | |
paul@181 | 2058 | if real_method_name == "<init>": |
paul@181 | 2059 | method_name = "__init__" |
paul@181 | 2060 | else: |
paul@181 | 2061 | method_name = real_method_name |
paul@181 | 2062 | |
paul@181 | 2063 | # Where only one method exists, just make an alias. |
paul@181 | 2064 | |
paul@181 | 2065 | if len(methods) == 1: |
paul@181 | 2066 | method, fn = methods[0] |
paul@181 | 2067 | self.namespace[method_name] = fn |
paul@181 | 2068 | return |
paul@181 | 2069 | |
paul@181 | 2070 | # Write a simple bytecode dispatching mechanism. |
paul@181 | 2071 | |
paul@181 | 2072 | program = BytecodeWriter() |
paul@181 | 2073 | |
paul@181 | 2074 | # Remember whether any of the methods are static. |
paul@181 | 2075 | # NOTE: This should be an all or nothing situation. |
paul@181 | 2076 | |
paul@181 | 2077 | method_is_static = 0 |
paul@181 | 2078 | |
paul@181 | 2079 | # NOTE: The code below should use dictionary-based dispatch for better performance. |
paul@181 | 2080 | |
paul@181 | 2081 | for method, fn in methods: |
paul@181 | 2082 | method_is_static = real_method_name != "<init>" and method_is_static or \ |
paul@181 | 2083 | classfile.has_flags(method.access_flags, [classfile.STATIC]) |
paul@181 | 2084 | |
paul@181 | 2085 | if method_is_static: |
paul@181 | 2086 | program.load_fast(0) # Stack: arguments |
paul@181 | 2087 | else: |
paul@181 | 2088 | program.load_fast(1) # Stack: arguments |
paul@181 | 2089 | |
paul@181 | 2090 | program.setup_loop() |
paul@181 | 2091 | program.load_const(1) # Stack: arguments, 1 |
paul@181 | 2092 | |
paul@181 | 2093 | if method_is_static: |
paul@181 | 2094 | program.store_fast(1) # Stack: arguments (found = 1) |
paul@181 | 2095 | else: |
paul@181 | 2096 | program.store_fast(2) # Stack: arguments (found = 1) |
paul@181 | 2097 | |
paul@181 | 2098 | # Emit a list of parameter types. |
paul@181 | 2099 | |
paul@181 | 2100 | descriptor_types = method.get_descriptor()[0] |
paul@181 | 2101 | for descriptor_type in descriptor_types: |
paul@181 | 2102 | base_type, object_type, array_type = descriptor_type |
paul@181 | 2103 | python_type = classfile.descriptor_base_type_mapping[base_type] |
paul@181 | 2104 | if python_type == "instance": |
paul@181 | 2105 | # NOTE: This will need extending. |
paul@181 | 2106 | python_type = object_type |
paul@181 | 2107 | program.load_global(python_type) # Stack: arguments, type, ... |
paul@181 | 2108 | program.build_list(len(descriptor_types)) |
paul@181 | 2109 | # Stack: arguments, types |
paul@181 | 2110 | # Make a map of arguments and types. |
paul@181 | 2111 | program.load_const(None) # Stack: arguments, types, None |
paul@181 | 2112 | program.rot_three() # Stack: None, arguments, types |
paul@181 | 2113 | program.build_tuple(3) # Stack: tuple |
paul@181 | 2114 | program.load_global("map") # Stack: tuple, map |
paul@181 | 2115 | program.rot_two() # Stack: map, tuple |
paul@181 | 2116 | program.call_function_var(0) # Stack: list (mapping arguments to types) |
paul@181 | 2117 | # Loop over each pair. |
paul@181 | 2118 | program.get_iter() # Stack: iter |
paul@181 | 2119 | program.for_iter() # Stack: iter, (argument, type) |
paul@181 | 2120 | program.unpack_sequence(2) # Stack: iter, type, argument |
paul@181 | 2121 | program.dup_top() # Stack: iter, type, argument, argument |
paul@181 | 2122 | program.load_const(None) # Stack: iter, type, argument, argument, None |
paul@181 | 2123 | program.compare_op("is") # Stack: iter, type, argument, result |
paul@181 | 2124 | # Missing argument? |
paul@181 | 2125 | program.jump_to_label(0, "present") |
paul@181 | 2126 | program.pop_top() # Stack: iter, type, argument |
paul@181 | 2127 | program.pop_top() # Stack: iter, type |
paul@181 | 2128 | program.pop_top() # Stack: iter |
paul@181 | 2129 | program.load_const(0) # Stack: iter, 0 |
paul@181 | 2130 | |
paul@181 | 2131 | if method_is_static: |
paul@181 | 2132 | program.store_fast(1) # Stack: iter (found = 0) |
paul@181 | 2133 | else: |
paul@181 | 2134 | program.store_fast(2) # Stack: iter (found = 0) |
paul@181 | 2135 | |
paul@181 | 2136 | program.break_loop() |
paul@181 | 2137 | # Argument was present. |
paul@181 | 2138 | program.start_label("present") |
paul@181 | 2139 | program.pop_top() # Stack: iter, type, argument |
paul@181 | 2140 | program.rot_two() # Stack: iter, argument, type |
paul@181 | 2141 | program.dup_top() # Stack: iter, argument, type, type |
paul@181 | 2142 | program.load_const(None) # Stack: iter, argument, type, type, None |
paul@181 | 2143 | program.compare_op("is") # Stack: iter, argument, type, result |
paul@181 | 2144 | # Missing parameter type? |
paul@181 | 2145 | program.jump_to_label(0, "present") |
paul@181 | 2146 | program.pop_top() # Stack: iter, argument, type |
paul@181 | 2147 | program.pop_top() # Stack: iter, argument |
paul@181 | 2148 | program.pop_top() # Stack: iter |
paul@181 | 2149 | program.load_const(0) # Stack: iter, 0 |
paul@181 | 2150 | |
paul@181 | 2151 | if method_is_static: |
paul@181 | 2152 | program.store_fast(1) # Stack: iter (found = 0) |
paul@181 | 2153 | else: |
paul@181 | 2154 | program.store_fast(2) # Stack: iter (found = 0) |
paul@181 | 2155 | |
paul@181 | 2156 | program.break_loop() |
paul@181 | 2157 | # Parameter was present. |
paul@181 | 2158 | program.start_label("present") |
paul@181 | 2159 | program.pop_top() # Stack: iter, argument, type |
paul@181 | 2160 | program.build_tuple(2) # Stack: iter, (argument, type) |
paul@181 | 2161 | program.load_global("isinstance") # Stack: iter, (argument, type), isinstance |
paul@181 | 2162 | program.rot_two() # Stack: iter, isinstance, (argument, type) |
paul@181 | 2163 | program.call_function_var(0) # Stack: iter, result |
paul@181 | 2164 | program.jump_to_label(1, "match") |
paul@181 | 2165 | program.pop_top() # Stack: iter |
paul@181 | 2166 | program.load_const(0) # Stack: iter, 0 |
paul@181 | 2167 | |
paul@181 | 2168 | if method_is_static: |
paul@181 | 2169 | program.store_fast(1) # Stack: iter (found = 0) |
paul@181 | 2170 | else: |
paul@181 | 2171 | program.store_fast(2) # Stack: iter (found = 0) |
paul@181 | 2172 | |
paul@181 | 2173 | program.break_loop() |
paul@181 | 2174 | # Argument type and parameter type matched. |
paul@181 | 2175 | program.start_label("match") |
paul@181 | 2176 | program.pop_top() # Stack: iter |
paul@181 | 2177 | program.end_loop() # Stack: |
paul@181 | 2178 | # If all the parameters matched, call the method. |
paul@181 | 2179 | |
paul@181 | 2180 | if method_is_static: |
paul@181 | 2181 | program.load_fast(1) # Stack: match |
paul@181 | 2182 | else: |
paul@181 | 2183 | program.load_fast(2) # Stack: match |
paul@181 | 2184 | |
paul@181 | 2185 | program.jump_to_label(0, "failed") |
paul@181 | 2186 | # All the parameters matched. |
paul@181 | 2187 | program.pop_top() # Stack: |
paul@181 | 2188 | |
paul@181 | 2189 | if method_is_static: |
paul@181 | 2190 | program.load_fast(0) # Stack: arguments |
paul@181 | 2191 | program.load_global(str(self.class_file.this_class.get_python_name())) |
paul@181 | 2192 | # Stack: arguments, class |
paul@181 | 2193 | else: |
paul@181 | 2194 | program.load_fast(1) # Stack: arguments |
paul@181 | 2195 | program.load_fast(0) # Stack: arguments, self |
paul@181 | 2196 | |
paul@181 | 2197 | program.load_attr(str(method.get_python_name())) |
paul@181 | 2198 | # Stack: arguments, method |
paul@181 | 2199 | program.rot_two() # Stack: method, arguments |
paul@181 | 2200 | program.call_function_var(0) # Stack: result |
paul@181 | 2201 | program.return_value() |
paul@181 | 2202 | # Try the next method if arguments or parameters were missing or incorrect. |
paul@181 | 2203 | program.start_label("failed") |
paul@181 | 2204 | program.pop_top() # Stack: |
paul@181 | 2205 | |
paul@181 | 2206 | # Raise an exception if nothing matched. |
paul@181 | 2207 | # NOTE: Improve this. |
paul@181 | 2208 | |
paul@181 | 2209 | program.load_const("No matching method") |
paul@181 | 2210 | program.raise_varargs(1) |
paul@181 | 2211 | program.load_const(None) |
paul@181 | 2212 | program.return_value() |
paul@181 | 2213 | |
paul@181 | 2214 | # Add the code as a method in the namespace. |
paul@181 | 2215 | # NOTE: One actual parameter, flags as 71 apparently means that a list |
paul@181 | 2216 | # NOTE: parameter is used in a method. |
paul@181 | 2217 | |
paul@181 | 2218 | if method_is_static: |
paul@181 | 2219 | nargs = 0 |
paul@181 | 2220 | else: |
paul@181 | 2221 | nargs = 1 |
paul@181 | 2222 | nlocals = program.max_locals + 1 |
paul@181 | 2223 | |
paul@181 | 2224 | code = new.code(nargs, nlocals, program.max_stack_depth, 71, program.get_output(), |
paul@181 | 2225 | tuple(program.get_constants()), tuple(program.get_names()), tuple(self.make_varnames(nlocals, method_is_static)), |
paul@181 | 2226 | self.filename, method_name, 0, "") |
paul@181 | 2227 | fn = new.function(code, global_names) |
paul@181 | 2228 | |
paul@181 | 2229 | if method_is_static: |
paul@181 | 2230 | fn = staticmethod(fn) |
paul@181 | 2231 | |
paul@181 | 2232 | self.namespace[method_name] = fn |
paul@181 | 2233 | |
paul@181 | 2234 | def process(self, global_names): |
paul@181 | 2235 | |
paul@181 | 2236 | """ |
paul@181 | 2237 | Process the class, storing it in the 'global_names' dictionary provided. |
paul@181 | 2238 | Return a list of external names referenced by the class's methods. |
paul@181 | 2239 | """ |
paul@181 | 2240 | |
paul@181 | 2241 | self.namespace = {} |
paul@181 | 2242 | |
paul@181 | 2243 | # Make the fields. |
paul@181 | 2244 | |
paul@181 | 2245 | for field in self.class_file.fields: |
paul@181 | 2246 | if classfile.has_flags(field.access_flags, [classfile.STATIC]): |
paul@181 | 2247 | field_name = str(field.get_python_name()) |
paul@181 | 2248 | self.namespace[field_name] = None |
paul@181 | 2249 | |
paul@181 | 2250 | # Make the methods. |
paul@181 | 2251 | |
paul@181 | 2252 | self.real_methods = {} |
paul@181 | 2253 | external_names = [] |
paul@181 | 2254 | |
paul@181 | 2255 | for method in self.class_file.methods: |
paul@181 | 2256 | real_method_name = str(method.get_name()) |
paul@181 | 2257 | method_name = str(method.get_python_name()) |
paul@181 | 2258 | translator, writer = self.translate_method(method) |
paul@181 | 2259 | |
paul@181 | 2260 | # Add external names to the master list. |
paul@181 | 2261 | |
paul@181 | 2262 | for external_name in writer.external_names: |
paul@181 | 2263 | if external_name not in external_names: |
paul@181 | 2264 | external_names.append(external_name) |
paul@181 | 2265 | |
paul@181 | 2266 | # Fix up special class initialisation methods and static methods. |
paul@181 | 2267 | |
paul@181 | 2268 | method_is_static = real_method_name != "<init>" and classfile.has_flags(method.access_flags, [classfile.STATIC]) |
paul@181 | 2269 | if method_is_static: |
paul@181 | 2270 | nargs = len(method.get_descriptor()[0]) |
paul@181 | 2271 | else: |
paul@181 | 2272 | nargs = len(method.get_descriptor()[0]) + 1 |
paul@181 | 2273 | nlocals = writer.max_locals + 1 |
paul@181 | 2274 | flags = 67 |
paul@181 | 2275 | |
paul@181 | 2276 | # NOTE: Add line number table later. |
paul@181 | 2277 | |
paul@181 | 2278 | code = new.code(nargs, nlocals, writer.max_stack_depth, flags, writer.get_output(), |
paul@181 | 2279 | tuple(writer.get_constants()), tuple(writer.get_names()), |
paul@181 | 2280 | tuple(self.make_varnames(nlocals, method_is_static)), self.filename, method_name, 0, "") |
paul@181 | 2281 | |
paul@181 | 2282 | # NOTE: May need more globals. |
paul@181 | 2283 | |
paul@181 | 2284 | fn = new.function(code, global_names) |
paul@181 | 2285 | |
paul@181 | 2286 | # Fix up special class initialisation methods and static methods. |
paul@181 | 2287 | |
paul@181 | 2288 | if method_is_static: |
paul@181 | 2289 | fn = staticmethod(fn) |
paul@181 | 2290 | |
paul@181 | 2291 | # Remember the real method name and the corresponding methods produced. |
paul@181 | 2292 | |
paul@181 | 2293 | if not self.real_methods.has_key(real_method_name): |
paul@181 | 2294 | self.real_methods[real_method_name] = [] |
paul@181 | 2295 | self.real_methods[real_method_name].append((method, fn)) |
paul@181 | 2296 | |
paul@181 | 2297 | # Add the method to the class's namespace. |
paul@181 | 2298 | |
paul@181 | 2299 | self.namespace[method_name] = fn |
paul@181 | 2300 | |
paul@181 | 2301 | # Add the super class as an external name, if appropriate. |
paul@181 | 2302 | |
paul@181 | 2303 | if self.class_file.super_class is not None: |
paul@181 | 2304 | external_names.append(self.class_file.super_class.get_python_name()) |
paul@181 | 2305 | |
paul@181 | 2306 | return external_names |
paul@181 | 2307 | |
paul@181 | 2308 | def get_class(self, global_names): |
paul@181 | 2309 | |
paul@181 | 2310 | """ |
paul@181 | 2311 | Get the Python class representing the underlying Java class, using the |
paul@181 | 2312 | given 'global_names' to define dispatcher methods. |
paul@181 | 2313 | """ |
paul@181 | 2314 | |
paul@181 | 2315 | # Define superclasses. |
paul@181 | 2316 | |
paul@181 | 2317 | bases = self.get_base_classes(global_names) |
paul@181 | 2318 | |
paul@181 | 2319 | # Define method dispatchers. |
paul@181 | 2320 | |
paul@181 | 2321 | for real_method_name, methods in self.real_methods.items(): |
paul@181 | 2322 | if real_method_name != "<clinit>": |
paul@181 | 2323 | self.make_method(real_method_name, methods, global_names) |
paul@181 | 2324 | |
paul@181 | 2325 | # Use only the last part of the fully qualified name. |
paul@181 | 2326 | |
paul@181 | 2327 | full_class_name = str(self.class_file.this_class.get_python_name()) |
paul@181 | 2328 | class_name = full_class_name.split(".")[-1] |
paul@181 | 2329 | cls = new.classobj(class_name, bases, self.namespace) |
paul@181 | 2330 | global_names[cls.__name__] = cls |
paul@181 | 2331 | |
paul@181 | 2332 | return cls |
paul@181 | 2333 | |
paul@181 | 2334 | def get_base_class_references(self): |
paul@181 | 2335 | class_names = [] |
paul@181 | 2336 | if self.class_file.super_class is not None: |
paul@181 | 2337 | class_names.append(self.class_file.super_class) |
paul@181 | 2338 | # NOTE: This prevents an MRO error, but may prevent proper functioning. |
paul@181 | 2339 | #class_names += self.class_file.interfaces |
paul@181 | 2340 | return class_names |
paul@181 | 2341 | |
paul@181 | 2342 | def get_base_classes(self, global_names): |
paul@181 | 2343 | |
paul@181 | 2344 | """ |
paul@181 | 2345 | Identify the superclass, then either load it from the given |
paul@181 | 2346 | 'global_names' if available, or import the class from its parent module. |
paul@181 | 2347 | Return a tuple containing all base classes (typically a single element |
paul@181 | 2348 | tuple). |
paul@181 | 2349 | """ |
paul@181 | 2350 | |
paul@181 | 2351 | base_classes = [] |
paul@181 | 2352 | |
paul@181 | 2353 | for class_ in self.get_base_class_references(): |
paul@181 | 2354 | base_classes.append(self._get_class(class_, global_names)) |
paul@181 | 2355 | |
paul@181 | 2356 | return tuple(base_classes) |
paul@181 | 2357 | |
paul@181 | 2358 | def _get_class(self, class_, global_names): |
paul@181 | 2359 | class_name = class_.get_python_name() |
paul@181 | 2360 | class_name_parts = class_name.split(".") |
paul@181 | 2361 | obj = global_names |
paul@181 | 2362 | for class_name_part in class_name_parts[:-1]: |
paul@181 | 2363 | try: |
paul@181 | 2364 | obj = obj[class_name_part].__dict__ |
paul@181 | 2365 | except KeyError: |
paul@181 | 2366 | raise AttributeError, "Cannot find '%s' when referencing Java class '%s'" % ( |
paul@181 | 2367 | class_name_part, class_name) |
paul@181 | 2368 | return obj[class_name_parts[-1]] |
paul@181 | 2369 | |
paul@181 | 2370 | def make_varnames(self, nlocals, method_is_static=0): |
paul@181 | 2371 | |
paul@181 | 2372 | """ |
paul@181 | 2373 | A utility method which invents variable names for the given number - |
paul@181 | 2374 | 'nlocals' - of local variables in a method. Returns a list of such |
paul@181 | 2375 | variable names. |
paul@181 | 2376 | |
paul@181 | 2377 | If the optional 'method_is_static' is set to true, do not use "self" as |
paul@181 | 2378 | the first argument name. |
paul@181 | 2379 | """ |
paul@181 | 2380 | |
paul@181 | 2381 | if method_is_static: |
paul@181 | 2382 | l = ["cls"] |
paul@181 | 2383 | else: |
paul@181 | 2384 | l = ["self"] |
paul@181 | 2385 | for i in range(1, nlocals): |
paul@181 | 2386 | l.append("_l%s" % i) |
paul@181 | 2387 | return l[:nlocals] |
paul@181 | 2388 | |
paul@181 | 2389 | # Test functions, useful for tracing generated bytecode operations. |
paul@181 | 2390 | |
paul@181 | 2391 | def _map(*args): |
paul@181 | 2392 | print args |
paul@181 | 2393 | return apply(__builtins__.map, args) |
paul@181 | 2394 | |
paul@181 | 2395 | def _isinstance(*args): |
paul@181 | 2396 | print args |
paul@181 | 2397 | return apply(__builtins__.isinstance, args) |
paul@181 | 2398 | |
paul@181 | 2399 | if __name__ == "__main__": |
paul@181 | 2400 | import sys |
paul@181 | 2401 | import dis |
paul@181 | 2402 | import java.lang |
paul@181 | 2403 | global_names = globals() |
paul@181 | 2404 | #global_names["isinstance"] = _isinstance |
paul@181 | 2405 | #global_names["map"] = _map |
paul@181 | 2406 | for filename in sys.argv[1:]: |
paul@181 | 2407 | f = open(filename, "rb") |
paul@181 | 2408 | c = classfile.ClassFile(f.read()) |
paul@181 | 2409 | translator = ClassTranslator(c) |
paul@181 | 2410 | external_names = translator.process(global_names) |
paul@181 | 2411 | cls = translator.get_class(global_names) |
paul@181 | 2412 | |
paul@181 | 2413 | # vim: tabstop=4 expandtab shiftwidth=4 |