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