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