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