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@4 | 10 | import dis # for access to Python bytecode values |
paul@6 | 11 | from UserDict import UserDict |
paul@4 | 12 | |
paul@5 | 13 | # Bytecode production classes. |
paul@5 | 14 | |
paul@5 | 15 | class BytecodeWriter: |
paul@5 | 16 | def __init__(self): |
paul@5 | 17 | self.loops = [] |
paul@6 | 18 | self.jumps = {} |
paul@5 | 19 | self.output = [] |
paul@5 | 20 | self.position = 0 |
paul@5 | 21 | |
paul@6 | 22 | # Mapping from values to indexes. |
paul@6 | 23 | self.constants = {} |
paul@6 | 24 | |
paul@6 | 25 | # Mapping from names to indexes. |
paul@6 | 26 | # NOTE: This may be acquired from elsewhere. |
paul@6 | 27 | self.globals = {} |
paul@6 | 28 | |
paul@6 | 29 | def get_output(self): |
paul@6 | 30 | output = [] |
paul@6 | 31 | for element in self.output: |
paul@6 | 32 | if isinstance(element, LazyValue): |
paul@6 | 33 | output.append(chr(element.value)) |
paul@6 | 34 | else: |
paul@6 | 35 | output.append(chr(element)) |
paul@6 | 36 | return "".join(output) |
paul@6 | 37 | |
paul@5 | 38 | # Special methods. |
paul@5 | 39 | |
paul@5 | 40 | def end_loop(self): |
paul@5 | 41 | current_loop_start = self.loops.pop() |
paul@5 | 42 | self.jump_absolute(current_loop_start) |
paul@5 | 43 | self.output[current_loop_start + 1] = self.position |
paul@5 | 44 | self.pop_block() |
paul@5 | 45 | |
paul@6 | 46 | def jump_to_label(self, status, name): |
paul@6 | 47 | # Record the instruction using the jump. |
paul@6 | 48 | if not self.jumps.has_key(name): |
paul@6 | 49 | self.jumps[name] = [] |
paul@6 | 50 | self.jumps[name].append(self.position) |
paul@6 | 51 | if status is None: |
paul@6 | 52 | self.jump_forward() |
paul@6 | 53 | elif status: |
paul@5 | 54 | self.jump_if_true() |
paul@5 | 55 | else: |
paul@5 | 56 | self.jump_if_false() |
paul@5 | 57 | |
paul@6 | 58 | def start_label(self, name): |
paul@6 | 59 | # Fill in all jump instructions. |
paul@6 | 60 | for jump_instruction in self.jumps[name]: |
paul@6 | 61 | self.output[jump_instruction + 1] = self.position |
paul@6 | 62 | |
paul@6 | 63 | # Complicated methods. |
paul@6 | 64 | |
paul@6 | 65 | def load_const(self, value): |
paul@6 | 66 | self.output.append(opmap["LOAD_CONST"]) |
paul@6 | 67 | if not self.constants.has_key(value): |
paul@6 | 68 | self.constants[value] = len(self.constants.keys()) |
paul@6 | 69 | self.output.append(self.constants[value]) |
paul@6 | 70 | |
paul@6 | 71 | def load_global(self, name): |
paul@6 | 72 | self.output.append(opmap["LOAD_GLOBAL"]) |
paul@6 | 73 | if not self.globals.has_key(value): |
paul@6 | 74 | self.globals[name] = len(self.globals.keys()) |
paul@6 | 75 | self.output.append(self.globals[name]) |
paul@6 | 76 | |
paul@6 | 77 | def load_fast(self, index): |
paul@6 | 78 | self.output.append(opmap["LOAD_FAST"]) |
paul@6 | 79 | self.output.append(index) |
paul@5 | 80 | |
paul@5 | 81 | # Normal bytecode generators. |
paul@5 | 82 | |
paul@5 | 83 | def for_iter(self): |
paul@5 | 84 | self.loops.push(self.position) |
paul@5 | 85 | self.output.append(opmap["FOR_ITER"]) |
paul@5 | 86 | self.output.append(None) # To be filled in later |
paul@5 | 87 | self.position += 2 |
paul@5 | 88 | |
paul@5 | 89 | def jump_if_false(self, offset=None): |
paul@5 | 90 | self.output.append(opmap["JUMP_IF_FALSE"]) |
paul@5 | 91 | self.output.append(offset) # May be filled in later |
paul@5 | 92 | self.position += 2 |
paul@5 | 93 | |
paul@5 | 94 | def jump_if_true(self, offset=None): |
paul@5 | 95 | self.output.append(opmap["JUMP_IF_TRUE"]) |
paul@5 | 96 | self.output.append(offset) # May be filled in later |
paul@5 | 97 | self.position += 2 |
paul@5 | 98 | |
paul@6 | 99 | def jump_forward(self, offset=None): |
paul@6 | 100 | self.output.append(opmap["JUMP_FORWARD"]) |
paul@6 | 101 | self.output.append(offset) # May be filled in later |
paul@6 | 102 | self.position += 2 |
paul@6 | 103 | |
paul@6 | 104 | # Utility classes and functions. |
paul@6 | 105 | |
paul@6 | 106 | class LazyDict(UserDict): |
paul@6 | 107 | def __getitem__(self, key): |
paul@6 | 108 | if not self.data.has_key(key): |
paul@6 | 109 | self.data[key] = LazyValue() |
paul@6 | 110 | return self.data[key] |
paul@6 | 111 | def __setitem__(self, key, value): |
paul@6 | 112 | if self.data.has_key(key): |
paul@6 | 113 | existing_value = self.data[key] |
paul@6 | 114 | if isinstance(existing_value, LazyValue): |
paul@6 | 115 | existing_value.value = value |
paul@6 | 116 | return |
paul@6 | 117 | self.data[key] = value |
paul@6 | 118 | |
paul@6 | 119 | class LazyValue: |
paul@6 | 120 | def __init__(self, value=None): |
paul@6 | 121 | self.value = value |
paul@6 | 122 | |
paul@6 | 123 | def signed(value, limit): |
paul@6 | 124 | |
paul@6 | 125 | """ |
paul@6 | 126 | Return the signed integer from the unsigned 'value', where 'limit' (a value |
paul@6 | 127 | one greater than the highest possible positive integer) is used to determine |
paul@6 | 128 | whether a negative or positive result is produced. |
paul@6 | 129 | """ |
paul@6 | 130 | |
paul@6 | 131 | d, r = divmod(value, limit) |
paul@6 | 132 | if d == 1: |
paul@6 | 133 | mask = limit * 2 - 1 |
paul@6 | 134 | return -1 - (value ^ mask) |
paul@6 | 135 | else: |
paul@6 | 136 | return value |
paul@6 | 137 | |
paul@6 | 138 | def signed2(value): |
paul@6 | 139 | return signed(value, 0x8000) |
paul@6 | 140 | |
paul@6 | 141 | def signed4(value): |
paul@6 | 142 | return signed(value, 0x80000000) |
paul@6 | 143 | |
paul@4 | 144 | # Bytecode conversion. |
paul@4 | 145 | |
paul@5 | 146 | class BytecodeReader: |
paul@5 | 147 | def __init__(self, class_file): |
paul@5 | 148 | self.class_file = class_file |
paul@6 | 149 | self.position_mapping = LazyDict() |
paul@5 | 150 | |
paul@5 | 151 | def process(self, code, program): |
paul@5 | 152 | self.java_position = 0 |
paul@5 | 153 | while self.java_position < len(code): |
paul@5 | 154 | self.position_mapping[self.java_position] = program.position |
paul@5 | 155 | bytecode = ord(code[self.java_position]) |
paul@5 | 156 | mnemonic, number_of_arguments = self.java_bytecodes[bytecode] |
paul@5 | 157 | self.process_bytecode(mnemonic, number_of_arguments) |
paul@5 | 158 | |
paul@5 | 159 | def process_bytecode(self, mnemonic, number_of_arguments): |
paul@5 | 160 | if number_of_arguments is not None: |
paul@5 | 161 | arguments = [] |
paul@5 | 162 | for j in range(0, number_of_arguments): |
paul@5 | 163 | arguments.append(ord(code[self.java_position + 1 + j])) |
paul@5 | 164 | |
paul@5 | 165 | # Call the handler. |
paul@5 | 166 | getattr(self, mnemonic)(arguments, program) |
paul@5 | 167 | else: |
paul@5 | 168 | # Call the handler. |
paul@5 | 169 | number_of_arguments = getattr(self, mnemonic)(code[self.java_position+1:], program) |
paul@5 | 170 | |
paul@5 | 171 | self.java_position = self.java_position + 1 + number_of_arguments |
paul@5 | 172 | |
paul@5 | 173 | def nop(self, arguments, program): |
paul@5 | 174 | pass |
paul@5 | 175 | |
paul@5 | 176 | def aaload(self, arguments, program): |
paul@5 | 177 | # NOTE: No type checking performed. |
paul@5 | 178 | program.binary_subscr() |
paul@5 | 179 | |
paul@5 | 180 | def aastore(self, arguments, program): |
paul@5 | 181 | # NOTE: No type checking performed. |
paul@5 | 182 | # Stack: arrayref, index, value |
paul@5 | 183 | program.rot_three() # Stack: value, arrayref, index |
paul@5 | 184 | program.store_subscr() |
paul@5 | 185 | |
paul@5 | 186 | def aconst_null(self, arguments, program): |
paul@5 | 187 | program.load_global(None) |
paul@5 | 188 | |
paul@5 | 189 | def aload(self, arguments, program): |
paul@5 | 190 | program.load_fast(arguments[0]) |
paul@5 | 191 | |
paul@5 | 192 | def aload_0(self, arguments, program): |
paul@5 | 193 | program.load_fast(0) |
paul@5 | 194 | |
paul@5 | 195 | def aload_1(self, arguments, program): |
paul@5 | 196 | program.load_fast(1) |
paul@5 | 197 | |
paul@5 | 198 | def aload_2(self, arguments, program): |
paul@5 | 199 | program.load_fast(2) |
paul@5 | 200 | |
paul@5 | 201 | def aload_3(self, arguments, program): |
paul@5 | 202 | program.load_fast(3) |
paul@5 | 203 | |
paul@5 | 204 | def anewarray(self, arguments, program): |
paul@5 | 205 | # NOTE: Does not raise NegativeArraySizeException. |
paul@5 | 206 | # NOTE: Not using the index to type the list/array. |
paul@5 | 207 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 208 | |
paul@6 | 209 | program.build_list() # Stack: count, list |
paul@6 | 210 | program.rot_two() # Stack: list, count |
paul@5 | 211 | program.setup_loop() |
paul@5 | 212 | program.load_global("range") |
paul@5 | 213 | program.load_const(0) # Stack: list, count, range, 0 |
paul@5 | 214 | program.rot_three() # Stack: list, 0, count, range |
paul@5 | 215 | program.rot_three() # Stack: list, range, 0, count |
paul@5 | 216 | program.call_function(2) # Stack: list, range_list |
paul@5 | 217 | program.get_iter() # Stack: list, iter |
paul@5 | 218 | program.for_iter() # Stack: list, iter, value |
paul@5 | 219 | program.pop_top() # Stack: list, iter |
paul@5 | 220 | program.rot_two() # Stack: iter, list |
paul@5 | 221 | program.dup_top() # Stack: iter, list, list |
paul@5 | 222 | program.load_attr("append") # Stack: iter, list, append |
paul@5 | 223 | program.load_global(None) # Stack: iter, list, append, None |
paul@5 | 224 | program.call_function(1) # Stack: iter, list, None |
paul@5 | 225 | program.pop_top() # Stack: iter, list |
paul@5 | 226 | program.rot_two() # Stack: list, iter |
paul@5 | 227 | program.end_loop() # Back to for_iter above |
paul@5 | 228 | |
paul@5 | 229 | def areturn(self, arguments, program): |
paul@5 | 230 | program.return_value() |
paul@5 | 231 | |
paul@5 | 232 | def arraylength(self, arguments, program): |
paul@5 | 233 | program.load_global("len") # Stack: arrayref, len |
paul@5 | 234 | program.rot_two() # Stack: len, arrayref |
paul@5 | 235 | program.call_function(1) |
paul@4 | 236 | |
paul@5 | 237 | def astore(self, arguments, program): |
paul@5 | 238 | program.store_fast(arguments[0]) |
paul@5 | 239 | |
paul@5 | 240 | def astore_0(self, arguments, program): |
paul@5 | 241 | program.store_fast(0) |
paul@5 | 242 | |
paul@5 | 243 | def astore_1(self, arguments, program): |
paul@5 | 244 | program.store_fast(1) |
paul@5 | 245 | |
paul@5 | 246 | def astore_2(self, arguments, program): |
paul@5 | 247 | program.store_fast(2) |
paul@5 | 248 | |
paul@5 | 249 | def astore_3(self, arguments, program): |
paul@5 | 250 | program.store_fast(3) |
paul@5 | 251 | |
paul@5 | 252 | def athrow(self, arguments, program): |
paul@5 | 253 | # NOTE: NullPointerException not raised where null/None is found on the stack. |
paul@5 | 254 | program.raise_varargs(1) |
paul@5 | 255 | |
paul@5 | 256 | baload = aaload |
paul@5 | 257 | bastore = aastore |
paul@5 | 258 | |
paul@5 | 259 | def bipush(self, arguments, program): |
paul@5 | 260 | program.load_const(arguments[0]) |
paul@5 | 261 | |
paul@5 | 262 | caload = aaload |
paul@5 | 263 | castore = aastore |
paul@5 | 264 | |
paul@5 | 265 | def checkcast(self, arguments, program): |
paul@5 | 266 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 267 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 268 | target_components = target_name.split("/") |
paul@5 | 269 | |
paul@5 | 270 | program.dup_top() # Stack: objectref, objectref |
paul@5 | 271 | program.load_global("isinstance") # Stack: objectref, objectref, isinstance |
paul@5 | 272 | program.rot_two() # Stack: objectref, isinstance, objectref |
paul@5 | 273 | program.load_global(target_components[0]) |
paul@5 | 274 | for target_component in target_components[1:]: |
paul@5 | 275 | program.load_attr(target_component) |
paul@5 | 276 | program.call_function(2) # Stack: objectref |
paul@5 | 277 | |
paul@5 | 278 | def d2f(self, arguments, program): |
paul@5 | 279 | pass |
paul@5 | 280 | |
paul@5 | 281 | def d2i(self, arguments, program): |
paul@5 | 282 | program.load_global("int") # Stack: value, int |
paul@5 | 283 | program.rot_two() # Stack: int, value |
paul@5 | 284 | program.call_function(1) # Stack: result |
paul@5 | 285 | |
paul@5 | 286 | d2l = d2i # Preserving Java semantics |
paul@5 | 287 | |
paul@5 | 288 | def dadd(self, arguments, program): |
paul@5 | 289 | # NOTE: No type checking performed. |
paul@5 | 290 | program.binary_add() |
paul@5 | 291 | |
paul@5 | 292 | daload = aaload |
paul@5 | 293 | dastore = aastore |
paul@5 | 294 | |
paul@5 | 295 | def dcmpg(self, arguments, program): |
paul@5 | 296 | # NOTE: No type checking performed. |
paul@5 | 297 | program.compare_op(">") |
paul@5 | 298 | |
paul@5 | 299 | def dcmpl(self, arguments, program): |
paul@5 | 300 | # NOTE: No type checking performed. |
paul@5 | 301 | program.compare_op("<") |
paul@5 | 302 | |
paul@5 | 303 | def dconst_0(self, arguments, program): |
paul@5 | 304 | program.load_const(0.0) |
paul@5 | 305 | |
paul@5 | 306 | def dconst_1(self, arguments, program): |
paul@5 | 307 | program.load_const(1.0) |
paul@5 | 308 | |
paul@5 | 309 | def ddiv(self, arguments, program): |
paul@5 | 310 | # NOTE: No type checking performed. |
paul@5 | 311 | program.binary_divide() |
paul@5 | 312 | |
paul@5 | 313 | dload = aload |
paul@5 | 314 | dload_0 = aload_0 |
paul@5 | 315 | dload_1 = aload_1 |
paul@5 | 316 | dload_2 = aload_2 |
paul@5 | 317 | dload_3 = aload_3 |
paul@5 | 318 | |
paul@5 | 319 | def dmul(self, arguments, program): |
paul@5 | 320 | # NOTE: No type checking performed. |
paul@5 | 321 | program.binary_multiply() |
paul@5 | 322 | |
paul@5 | 323 | def dneg(self, arguments, program): |
paul@5 | 324 | # NOTE: No type checking performed. |
paul@5 | 325 | program.unary_negative() |
paul@4 | 326 | |
paul@5 | 327 | def drem(self, arguments, program): |
paul@5 | 328 | # NOTE: No type checking performed. |
paul@5 | 329 | program.binary_modulo() |
paul@5 | 330 | |
paul@5 | 331 | dreturn = areturn |
paul@5 | 332 | dstore = astore |
paul@5 | 333 | dstore_0 = astore_0 |
paul@5 | 334 | dstore_1 = astore_1 |
paul@5 | 335 | dstore_2 = astore_2 |
paul@5 | 336 | dstore_3 = astore_3 |
paul@5 | 337 | |
paul@5 | 338 | def dsub(self, arguments, program): |
paul@5 | 339 | # NOTE: No type checking performed. |
paul@5 | 340 | program.binary_subtract() |
paul@5 | 341 | |
paul@5 | 342 | def dup(self, arguments, program): |
paul@5 | 343 | program.dup_top() |
paul@5 | 344 | |
paul@5 | 345 | def dup_x1(self, arguments, program): |
paul@5 | 346 | # Ignoring computational type categories. |
paul@5 | 347 | program.dup_top() |
paul@5 | 348 | program.rot_three() |
paul@5 | 349 | |
paul@5 | 350 | def dup_x2(self, arguments, program): |
paul@5 | 351 | # Ignoring computational type categories. |
paul@5 | 352 | program.dup_top() |
paul@5 | 353 | program.rot_four() |
paul@5 | 354 | |
paul@5 | 355 | dup2 = dup # Ignoring computational type categories |
paul@5 | 356 | dup2_x1 = dup_x1 # Ignoring computational type categories |
paul@5 | 357 | dup2_x2 = dup_x2 # Ignoring computational type categories |
paul@5 | 358 | |
paul@5 | 359 | def f2d(self, arguments, program): |
paul@5 | 360 | pass # Preserving Java semantics |
paul@5 | 361 | |
paul@5 | 362 | def f2i(self, arguments, program): |
paul@5 | 363 | program.load_global("int") # Stack: value, int |
paul@5 | 364 | program.rot_two() # Stack: int, value |
paul@5 | 365 | program.call_function(1) # Stack: result |
paul@5 | 366 | |
paul@5 | 367 | f2l = f2i # Preserving Java semantics |
paul@5 | 368 | fadd = dadd |
paul@5 | 369 | faload = daload |
paul@5 | 370 | fastore = dastore |
paul@5 | 371 | fcmpg = dcmpg |
paul@5 | 372 | fcmpl = dcmpl |
paul@5 | 373 | fconst_0 = dconst_0 |
paul@5 | 374 | fconst_1 = dconst_1 |
paul@5 | 375 | |
paul@5 | 376 | def fconst_2(self, arguments, program): |
paul@5 | 377 | program.load_const(2.0) |
paul@5 | 378 | |
paul@5 | 379 | fdiv = ddiv |
paul@5 | 380 | fload = dload |
paul@5 | 381 | fload_0 = dload_0 |
paul@5 | 382 | fload_1 = dload_1 |
paul@5 | 383 | fload_2 = dload_2 |
paul@5 | 384 | fload_3 = dload_3 |
paul@5 | 385 | fmul = dmul |
paul@5 | 386 | fneg = dneg |
paul@5 | 387 | frem = drem |
paul@5 | 388 | freturn = dreturn |
paul@5 | 389 | fstore = dstore |
paul@5 | 390 | fstore_0 = dstore_0 |
paul@5 | 391 | fstore_1 = dstore_1 |
paul@5 | 392 | fstore_2 = dstore_2 |
paul@5 | 393 | fstore_3 = dstore_3 |
paul@5 | 394 | fsub = dsub |
paul@5 | 395 | |
paul@5 | 396 | def getfield(self, arguments, program): |
paul@5 | 397 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 398 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 399 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@5 | 400 | program.load_attr(str(target_name)) |
paul@5 | 401 | |
paul@5 | 402 | getstatic = getfield # Ignoring Java restrictions |
paul@5 | 403 | |
paul@5 | 404 | def goto(self, arguments, program): |
paul@6 | 405 | offset = signed2(arguments[0] << 8 + arguments[1]) |
paul@5 | 406 | java_absolute = self.java_position + offset |
paul@5 | 407 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@5 | 408 | |
paul@5 | 409 | def goto_w(self, arguments, program): |
paul@6 | 410 | offset = signed4(arguments[0] << 24 + arguments[1] << 16 + arguments[2] << 8 + arguments[3]) |
paul@5 | 411 | java_absolute = self.java_position + offset |
paul@5 | 412 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@5 | 413 | |
paul@5 | 414 | def i2b(self, arguments, program): |
paul@5 | 415 | pass |
paul@4 | 416 | |
paul@5 | 417 | def i2c(self, arguments, program): |
paul@5 | 418 | program.load_global("chr") # Stack: value, chr |
paul@5 | 419 | program.rot_two() # Stack: chr, value |
paul@5 | 420 | program.call_function(1) # Stack: result |
paul@5 | 421 | |
paul@5 | 422 | def i2d(self, arguments, program): |
paul@5 | 423 | program.load_global("float") # Stack: value, float |
paul@5 | 424 | program.rot_two() # Stack: float, value |
paul@5 | 425 | program.call_function(1) # Stack: result |
paul@5 | 426 | |
paul@5 | 427 | i2f = i2d # Not distinguishing between float and double |
paul@5 | 428 | |
paul@5 | 429 | def i2l(self, arguments, program): |
paul@5 | 430 | pass # Preserving Java semantics |
paul@5 | 431 | |
paul@5 | 432 | def i2s(self, arguments, program): |
paul@5 | 433 | pass # Not distinguishing between int and short |
paul@5 | 434 | |
paul@5 | 435 | iadd = fadd |
paul@5 | 436 | iaload = faload |
paul@5 | 437 | |
paul@5 | 438 | def iand(self, arguments, program): |
paul@5 | 439 | # NOTE: No type checking performed. |
paul@5 | 440 | program.binary_and() |
paul@5 | 441 | |
paul@5 | 442 | iastore = fastore |
paul@5 | 443 | |
paul@5 | 444 | def iconst_m1(self, arguments, program): |
paul@5 | 445 | program.load_const(-1) |
paul@5 | 446 | |
paul@5 | 447 | def iconst_0(self, arguments, program): |
paul@5 | 448 | program.load_const(0) |
paul@5 | 449 | |
paul@5 | 450 | def iconst_1(self, arguments, program): |
paul@5 | 451 | program.load_const(1) |
paul@5 | 452 | |
paul@5 | 453 | def iconst_2(self, arguments, program): |
paul@5 | 454 | program.load_const(2) |
paul@5 | 455 | |
paul@5 | 456 | def iconst_3(self, arguments, program): |
paul@5 | 457 | program.load_const(3) |
paul@5 | 458 | |
paul@5 | 459 | def iconst_4(self, arguments, program): |
paul@5 | 460 | program.load_const(4) |
paul@4 | 461 | |
paul@5 | 462 | def iconst_5(self, arguments, program): |
paul@5 | 463 | program.load_const(5) |
paul@5 | 464 | |
paul@5 | 465 | idiv = fdiv |
paul@5 | 466 | |
paul@5 | 467 | def _if_xcmpx(self, arguments, program, op): |
paul@6 | 468 | offset = signed2(arguments[0] << 8 + arguments[1]) |
paul@5 | 469 | java_absolute = self.java_position + offset |
paul@5 | 470 | program.compare_op(op) |
paul@6 | 471 | program.jump_to_label(0, "next") # skip if false |
paul@5 | 472 | program.goto(offset) |
paul@6 | 473 | program.start_label("next") |
paul@5 | 474 | |
paul@5 | 475 | def if_acmpeq(self, arguments, program): |
paul@5 | 476 | # NOTE: No type checking performed. |
paul@5 | 477 | self._if_xcmpx(arguments, program, "is") |
paul@5 | 478 | |
paul@5 | 479 | def if_acmpne(self, arguments, program): |
paul@5 | 480 | # NOTE: No type checking performed. |
paul@5 | 481 | self._if_xcmpx(arguments, program, "is not") |
paul@4 | 482 | |
paul@5 | 483 | def if_icmpeq(self, arguments, program): |
paul@5 | 484 | # NOTE: No type checking performed. |
paul@5 | 485 | self._if_xcmpx(arguments, program, "==") |
paul@5 | 486 | |
paul@5 | 487 | def if_icmpne(self, arguments, program): |
paul@5 | 488 | # NOTE: No type checking performed. |
paul@5 | 489 | self._if_xcmpx(arguments, program, "!=") |
paul@5 | 490 | |
paul@5 | 491 | def if_icmplt(self, arguments, program): |
paul@5 | 492 | # NOTE: No type checking performed. |
paul@5 | 493 | self._if_xcmpx(arguments, program, "<") |
paul@5 | 494 | |
paul@5 | 495 | def if_icmpge(self, arguments, program): |
paul@5 | 496 | # NOTE: No type checking performed. |
paul@5 | 497 | self._if_xcmpx(arguments, program, ">=") |
paul@5 | 498 | |
paul@5 | 499 | def if_icmpgt(self, arguments, program): |
paul@5 | 500 | # NOTE: No type checking performed. |
paul@5 | 501 | self._if_xcmpx(arguments, program, ">") |
paul@5 | 502 | |
paul@5 | 503 | def if_icmple(self, arguments, program): |
paul@5 | 504 | # NOTE: No type checking performed. |
paul@5 | 505 | self._if_xcmpx(arguments, program, "<=") |
paul@4 | 506 | |
paul@5 | 507 | def ifeq(self, arguments, program): |
paul@5 | 508 | # NOTE: No type checking performed. |
paul@5 | 509 | program.load_const(0) |
paul@5 | 510 | self._if_xcmpx(arguments, program, "==") |
paul@5 | 511 | |
paul@5 | 512 | def ifne(self, arguments, program): |
paul@5 | 513 | # NOTE: No type checking performed. |
paul@5 | 514 | program.load_const(0) |
paul@5 | 515 | self._if_xcmpx(arguments, program, "!=") |
paul@5 | 516 | |
paul@5 | 517 | def iflt(self, arguments, program): |
paul@5 | 518 | # NOTE: No type checking performed. |
paul@5 | 519 | program.load_const(0) |
paul@5 | 520 | self._if_xcmpx(arguments, program, "<") |
paul@5 | 521 | |
paul@5 | 522 | def ifge(self, arguments, program): |
paul@5 | 523 | # NOTE: No type checking performed. |
paul@5 | 524 | program.load_const(0) |
paul@5 | 525 | self._if_xcmpx(arguments, program, ">=") |
paul@5 | 526 | |
paul@5 | 527 | def ifgt(self, arguments, program): |
paul@5 | 528 | # NOTE: No type checking performed. |
paul@5 | 529 | program.load_const(0) |
paul@5 | 530 | self._if_xcmpx(arguments, program, ">") |
paul@5 | 531 | |
paul@5 | 532 | def ifle(self, arguments, program): |
paul@5 | 533 | # NOTE: No type checking performed. |
paul@5 | 534 | program.load_const(0) |
paul@5 | 535 | self._if_xcmpx(arguments, program, "<=") |
paul@5 | 536 | |
paul@5 | 537 | def ifnonnull(self, arguments, program): |
paul@5 | 538 | # NOTE: No type checking performed. |
paul@5 | 539 | program.load_const(None) |
paul@5 | 540 | self._if_xcmpx(arguments, program, "is not") |
paul@5 | 541 | |
paul@5 | 542 | def ifnull(self, arguments, program): |
paul@5 | 543 | # NOTE: No type checking performed. |
paul@5 | 544 | program.load_const(None) |
paul@5 | 545 | self._if_xcmpx(arguments, program, "is") |
paul@5 | 546 | |
paul@5 | 547 | def iinc(self, arguments, program): |
paul@5 | 548 | # NOTE: No type checking performed. |
paul@5 | 549 | program.load_fast(arguments[0]) |
paul@5 | 550 | program.load_const(arguments[1]) |
paul@5 | 551 | program.binary_add() |
paul@5 | 552 | |
paul@5 | 553 | iload = fload |
paul@5 | 554 | iload_0 = fload_0 |
paul@5 | 555 | iload_1 = fload_1 |
paul@5 | 556 | iload_2 = fload_2 |
paul@5 | 557 | iload_3 = fload_3 |
paul@5 | 558 | imul = fmul |
paul@5 | 559 | ineg = fneg |
paul@5 | 560 | |
paul@5 | 561 | def instanceof(self, arguments, program): |
paul@5 | 562 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 563 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 564 | target_components = target_name.split("/") |
paul@5 | 565 | |
paul@5 | 566 | program.load_global("isinstance") # Stack: objectref, isinstance |
paul@5 | 567 | program.rot_two() # Stack: isinstance, objectref |
paul@5 | 568 | program.load_global(target_components[0]) |
paul@5 | 569 | for target_component in target_components[1:]: |
paul@5 | 570 | program.load_attr(target_component) |
paul@5 | 571 | program.call_function(2) # Stack: result |
paul@5 | 572 | |
paul@5 | 573 | def _invoke(self, target_name, program): |
paul@5 | 574 | program.rot_two() # Stack: tuple, objectref |
paul@5 | 575 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@5 | 576 | program.load_attr(str(target_name)) # Stack: tuple, method |
paul@5 | 577 | program.rot_two() # Stack: method, tuple |
paul@5 | 578 | program.load_global("apply") # Stack: method, tuple, apply |
paul@5 | 579 | program.rot_three() # Stack: apply, method, tuple |
paul@5 | 580 | program.call_function(2) |
paul@5 | 581 | |
paul@5 | 582 | def invokeinterface(self, arguments, program): |
paul@5 | 583 | # NOTE: This implementation does not perform the necessary checks for |
paul@5 | 584 | # NOTE: signature-based polymorphism. |
paul@5 | 585 | # NOTE: Java rules not specifically obeyed. |
paul@5 | 586 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 587 | count = arguments[2] |
paul@5 | 588 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 589 | # Stack: objectref, arg1, arg2, ... |
paul@5 | 590 | program.build_tuple(count) # Stack: objectref, tuple |
paul@5 | 591 | self._invoke(target_name, program) |
paul@5 | 592 | |
paul@5 | 593 | def invokespecial(self, arguments, program): |
paul@5 | 594 | # NOTE: This implementation does not perform the necessary checks for |
paul@5 | 595 | # NOTE: signature-based polymorphism. |
paul@5 | 596 | # NOTE: Java rules not specifically obeyed. |
paul@5 | 597 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 598 | target = self.class_file.constants[index - 1] |
paul@5 | 599 | target_name = target.get_name() |
paul@5 | 600 | # Get the number of parameters from the descriptor. |
paul@5 | 601 | count = len(target.get_descriptor()[0]) |
paul@5 | 602 | # Stack: objectref, arg1, arg2, ... |
paul@5 | 603 | program.build_tuple(count) # Stack: objectref, tuple |
paul@5 | 604 | self._invoke(target_name, program) |
paul@5 | 605 | |
paul@5 | 606 | def invokestatic(self, arguments, program): |
paul@5 | 607 | # NOTE: This implementation does not perform the necessary checks for |
paul@5 | 608 | # NOTE: signature-based polymorphism. |
paul@5 | 609 | # NOTE: Java rules not specifically obeyed. |
paul@5 | 610 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 611 | target = self.class_file.constants[index - 1] |
paul@5 | 612 | target_name = target.get_name() |
paul@5 | 613 | # Get the number of parameters from the descriptor. |
paul@5 | 614 | count = len(target.get_descriptor()[0]) |
paul@5 | 615 | # Stack: arg1, arg2, ... |
paul@5 | 616 | program.build_tuple(count) # Stack: tuple |
paul@5 | 617 | # NOTE: Should probably use Python static methods. |
paul@5 | 618 | program.load_name("self") # Stack: tuple, self |
paul@5 | 619 | self._invoke(target_name, program) |
paul@5 | 620 | |
paul@5 | 621 | invokevirtual = invokeinterface # Ignoring Java rules |
paul@5 | 622 | |
paul@5 | 623 | def ior(self, arguments, program): |
paul@5 | 624 | # NOTE: No type checking performed. |
paul@5 | 625 | program.binary_or() |
paul@5 | 626 | |
paul@5 | 627 | irem = frem |
paul@5 | 628 | ireturn = freturn |
paul@5 | 629 | |
paul@5 | 630 | def ishl(self, arguments, program): |
paul@5 | 631 | # NOTE: No type checking performed. |
paul@5 | 632 | # NOTE: Not verified. |
paul@5 | 633 | program.binary_lshift() |
paul@5 | 634 | |
paul@5 | 635 | def ishr(self, arguments, program): |
paul@5 | 636 | # NOTE: No type checking performed. |
paul@5 | 637 | # NOTE: Not verified. |
paul@5 | 638 | program.binary_rshift() |
paul@5 | 639 | |
paul@5 | 640 | istore = fstore |
paul@5 | 641 | istore_0 = fstore_0 |
paul@5 | 642 | istore_1 = fstore_1 |
paul@5 | 643 | istore_2 = fstore_2 |
paul@5 | 644 | istore_3 = fstore_3 |
paul@5 | 645 | isub = fsub |
paul@5 | 646 | iushr = ishr # Ignoring distinctions between arithmetic and logical shifts |
paul@5 | 647 | |
paul@6 | 648 | def ixor(self, arguments, program): |
paul@5 | 649 | # NOTE: No type checking performed. |
paul@5 | 650 | program.binary_xor() |
paul@5 | 651 | |
paul@5 | 652 | def jsr(self, arguments, program): |
paul@6 | 653 | offset = signed2(arguments[0] << 8 + arguments[1]) |
paul@6 | 654 | java_absolute = self.java_position + offset |
paul@6 | 655 | # Store the address of the next instruction. |
paul@6 | 656 | program.load_const(self.position_mapping[self.java_position + 3]) |
paul@6 | 657 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@6 | 658 | |
paul@6 | 659 | def jsr_w(self, arguments, program): |
paul@6 | 660 | offset = signed4(arguments[0] << 24 + arguments[1] << 16 + arguments[2] << 8 + arguments[3]) |
paul@5 | 661 | java_absolute = self.java_position + offset |
paul@6 | 662 | # Store the address of the next instruction. |
paul@6 | 663 | program.load_const(self.position_mapping[self.java_position + 5]) |
paul@6 | 664 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@6 | 665 | |
paul@6 | 666 | l2d = i2d |
paul@6 | 667 | l2f = i2f |
paul@6 | 668 | |
paul@6 | 669 | def l2i(self, arguments, program): |
paul@6 | 670 | pass # Preserving Java semantics |
paul@6 | 671 | |
paul@6 | 672 | ladd = iadd |
paul@6 | 673 | laload = iaload |
paul@6 | 674 | land = iand |
paul@6 | 675 | lastore = iastore |
paul@6 | 676 | |
paul@6 | 677 | def lcmp(self, arguments, program): |
paul@6 | 678 | # NOTE: No type checking performed. |
paul@6 | 679 | program.dup_topx(2) # Stack: value1, value2, value1, value2 |
paul@6 | 680 | program.compare_op(">") # Stack: value1, value2, result |
paul@6 | 681 | program.jump_to_label(0, "equals") |
paul@6 | 682 | # True - produce result and branch. |
paul@6 | 683 | program.pop_top() # Stack: value1, value2 |
paul@6 | 684 | program.pop_top() # Stack: value1 |
paul@6 | 685 | program.pop_top() # Stack: |
paul@6 | 686 | program.load_const(1) # Stack: 1 |
paul@6 | 687 | program.jump_to_label(None, "next") |
paul@6 | 688 | # False - test equality. |
paul@6 | 689 | program.start_label("equals") |
paul@6 | 690 | program.pop_top() # Stack: value1, value2 |
paul@6 | 691 | program.dup_topx(2) # Stack: value1, value2, value1, value2 |
paul@6 | 692 | program.compare_op("==") # Stack: value1, value2, result |
paul@6 | 693 | program.jump_to_label(0, "less") |
paul@6 | 694 | # True - produce result and branch. |
paul@6 | 695 | program.pop_top() # Stack: value1, value2 |
paul@6 | 696 | program.pop_top() # Stack: value1 |
paul@6 | 697 | program.pop_top() # Stack: |
paul@6 | 698 | program.load_const(0) # Stack: 0 |
paul@6 | 699 | program.jump_to_label(None, "next") |
paul@6 | 700 | # False - produce result. |
paul@6 | 701 | program.start_label("less") |
paul@6 | 702 | program.pop_top() # Stack: value1, value2 |
paul@6 | 703 | program.pop_top() # Stack: value1 |
paul@6 | 704 | program.pop_top() # Stack: |
paul@6 | 705 | program.load_const(-1) # Stack: -1 |
paul@6 | 706 | program.start_label("next") |
paul@6 | 707 | |
paul@6 | 708 | lconst_0 = iconst_0 |
paul@6 | 709 | lconst_1 = iconst_1 |
paul@6 | 710 | |
paul@6 | 711 | def ldc(self, arguments, program): |
paul@6 | 712 | program.load_const(self.class_file.constants[arguments[0] - 1]) |
paul@6 | 713 | |
paul@6 | 714 | def ldc_w(self, arguments, program): |
paul@6 | 715 | program.load_const(self.class_file.constants[arguments[0] << 8 + arguments[1] - 1]) |
paul@6 | 716 | |
paul@6 | 717 | ldc2_w = ldc_w |
paul@6 | 718 | ldiv = idiv |
paul@6 | 719 | lload = iload |
paul@6 | 720 | lload_0 = iload_0 |
paul@6 | 721 | lload_1 = iload_1 |
paul@6 | 722 | lload_2 = iload_2 |
paul@6 | 723 | lload_3 = iload_3 |
paul@6 | 724 | lmul = imul |
paul@6 | 725 | lneg = ineg |
paul@6 | 726 | |
paul@6 | 727 | def lookupswitch(self, arguments, program): |
paul@6 | 728 | # Find the offset to the next 4 byte boundary in the code. |
paul@6 | 729 | d, r = divmod(self.java_position, 4) |
paul@6 | 730 | to_boundary = (4 - r) % 4 |
paul@6 | 731 | # Get the pertinent arguments. |
paul@6 | 732 | arguments = arguments[to_boundary:] |
paul@6 | 733 | default = arguments[0] << 24 + arguments[1] << 16 + arguments[2] << 8 + arguments[3] |
paul@6 | 734 | npairs = arguments[4] << 24 + arguments[5] << 16 + arguments[6] << 8 + arguments[7] |
paul@6 | 735 | # Process the pairs. |
paul@6 | 736 | # NOTE: This is not the most optimal implementation. |
paul@6 | 737 | pair_index = 8 |
paul@6 | 738 | for pair in range(0, npairs): |
paul@6 | 739 | match = (arguments[pair_index] << 24 + arguments[pair_index + 1] << 16 + |
paul@6 | 740 | arguments[pair_index + 2] << 8 + arguments[pair_index + 3]) |
paul@6 | 741 | offset = signed4(arguments[pair_index + 4] << 24 + arguments[pair_index + 5] << 16 + |
paul@6 | 742 | arguments[pair_index + 6] << 8 + arguments[pair_index + 7]) |
paul@6 | 743 | # Calculate the branch target. |
paul@6 | 744 | java_absolute = self.java_position + offset |
paul@6 | 745 | # Generate branching code. |
paul@6 | 746 | program.dup_top() # Stack: key, key |
paul@6 | 747 | program.load_const(match) # Stack: key, key, match |
paul@6 | 748 | program.compare_op("==") # Stack: key, result |
paul@6 | 749 | program.jump_to_label(0, "end" + str(pair)) |
paul@6 | 750 | program.pop_top() # Stack: key |
paul@6 | 751 | program.pop_top() # Stack: |
paul@6 | 752 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@6 | 753 | # Generate the label for the end of the branching code. |
paul@6 | 754 | program.start_label("end" + str(pair)) |
paul@6 | 755 | program.pop_top() # Stack: key |
paul@6 | 756 | # Update the index. |
paul@6 | 757 | pair_index += 8 |
paul@6 | 758 | # Generate the default. |
paul@6 | 759 | java_absolute = self.java_position + default |
paul@6 | 760 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@6 | 761 | |
paul@6 | 762 | lor = ior |
paul@6 | 763 | lrem = irem |
paul@6 | 764 | lreturn = ireturn |
paul@6 | 765 | lshl = ishl |
paul@6 | 766 | lshr = ishr |
paul@6 | 767 | lstore = istore |
paul@6 | 768 | lstore_0 = istore_0 |
paul@6 | 769 | lstore_1 = istore_1 |
paul@6 | 770 | lstore_2 = istore_2 |
paul@6 | 771 | lstore_3 = istore_3 |
paul@6 | 772 | lsub = isub |
paul@6 | 773 | lushr = iushr |
paul@6 | 774 | lxor = ixor |
paul@6 | 775 | |
paul@6 | 776 | def monitorenter(self, arguments, program): |
paul@5 | 777 | # NOTE: To be implemented. |
paul@6 | 778 | pass |
paul@6 | 779 | |
paul@6 | 780 | def monitorexit(self, arguments, program): |
paul@6 | 781 | # NOTE: To be implemented. |
paul@6 | 782 | pass |
paul@6 | 783 | |
paul@6 | 784 | def multianewarray(self, arguments, program): |
paul@6 | 785 | program.build_list() # Stack: count1, count2, ..., countN, list |
paul@6 | 786 | program.rot_two() # Stack: count1, count2, ..., list, countN |
paul@6 | 787 | program.setup_loop() |
paul@6 | 788 | program.load_global("range") |
paul@6 | 789 | program.load_const(0) # Stack: list, count, range, 0 |
paul@6 | 790 | program.rot_three() # Stack: list, 0, count, range |
paul@6 | 791 | program.rot_three() # Stack: list, range, 0, count |
paul@6 | 792 | program.call_function(2) # Stack: list, range_list |
paul@6 | 793 | program.get_iter() # Stack: list, iter |
paul@6 | 794 | program.for_iter() # Stack: list, iter, value |
paul@6 | 795 | for i in range(0, arguments[2]): |
paul@6 | 796 | # Stack: |
paul@6 | 797 | self.anewarray(arguments, program) # Stack: list, iter |
paul@5 | 798 | |
paul@5 | 799 | def wide(self, code, program): |
paul@5 | 800 | # NOTE: To be implemented. |
paul@5 | 801 | return number_of_arguments |
paul@5 | 802 | |
paul@5 | 803 | java_bytecodes = { |
paul@5 | 804 | # code : (mnemonic, number of following bytes, change in stack) |
paul@5 | 805 | 0 : ("nop", 0), |
paul@5 | 806 | 1 : ("aconst_null", 0), |
paul@5 | 807 | 2 : ("iconst_m1", 0), |
paul@5 | 808 | 3 : ("iconst_0", 0), |
paul@5 | 809 | 4 : ("iconst_1", 0), |
paul@5 | 810 | 5 : ("iconst_2", 0), |
paul@5 | 811 | 6 : ("iconst_3", 0), |
paul@5 | 812 | 7 : ("iconst_4", 0), |
paul@5 | 813 | 8 : ("iconst_5", 0), |
paul@5 | 814 | 9 : ("lconst_0", 0), |
paul@5 | 815 | 10 : ("lconst_1", 0), |
paul@5 | 816 | 11 : ("fconst_0", 0), |
paul@5 | 817 | 12 : ("fconst_1", 0), |
paul@5 | 818 | 13 : ("fconst_2", 0), |
paul@5 | 819 | 14 : ("dconst_0", 0), |
paul@5 | 820 | 15 : ("dconst_1", 0), |
paul@5 | 821 | 16 : ("bipush", 1), |
paul@5 | 822 | 17 : ("sipush", 2), |
paul@5 | 823 | 18 : ("ldc", 1), |
paul@5 | 824 | 19 : ("ldc_w", 2), |
paul@5 | 825 | 20 : ("ldc2_w", 2), |
paul@5 | 826 | 21 : ("iload", 1), |
paul@5 | 827 | 22 : ("lload", 1), |
paul@5 | 828 | 23 : ("fload", 1), |
paul@5 | 829 | 24 : ("dload", 1), |
paul@5 | 830 | 25 : ("aload", 1), |
paul@5 | 831 | 26 : ("iload_0", 0), |
paul@5 | 832 | 27 : ("iload_1", 0), |
paul@5 | 833 | 28 : ("iload_2", 0), |
paul@5 | 834 | 29 : ("iload_3", 0), |
paul@5 | 835 | 30 : ("lload_0", 0), |
paul@5 | 836 | 31 : ("lload_1", 0), |
paul@5 | 837 | 32 : ("lload_2", 0), |
paul@5 | 838 | 33 : ("lload_3", 0), |
paul@5 | 839 | 34 : ("fload_0", 0), |
paul@5 | 840 | 35 : ("fload_1", 0), |
paul@5 | 841 | 36 : ("fload_2", 0), |
paul@5 | 842 | 37 : ("fload_3", 0), |
paul@5 | 843 | 38 : ("dload_0", 0), |
paul@5 | 844 | 39 : ("dload_1", 0), |
paul@5 | 845 | 40 : ("dload_2", 0), |
paul@5 | 846 | 41 : ("dload_3", 0), |
paul@5 | 847 | 42 : ("aload_0", 0), |
paul@5 | 848 | 43 : ("aload_1", 0), |
paul@5 | 849 | 44 : ("aload_2", 0), |
paul@5 | 850 | 45 : ("aload_3", 0), |
paul@5 | 851 | 46 : ("iaload", 0), |
paul@5 | 852 | 47 : ("laload", 0), |
paul@5 | 853 | 48 : ("faload", 0), |
paul@5 | 854 | 49 : ("daload", 0), |
paul@5 | 855 | 50 : ("aaload", 0), |
paul@5 | 856 | 51 : ("baload", 0), |
paul@5 | 857 | 52 : ("caload", 0), |
paul@5 | 858 | 53 : ("saload", 0), |
paul@5 | 859 | 54 : ("istore", 1), |
paul@5 | 860 | 55 : ("lstore", 1), |
paul@5 | 861 | 56 : ("fstore", 1), |
paul@5 | 862 | 57 : ("dstore", 1), |
paul@5 | 863 | 58 : ("astore", 1), |
paul@5 | 864 | 59 : ("istore_0", 0), |
paul@5 | 865 | 60 : ("istore_1", 0), |
paul@5 | 866 | 61 : ("istore_2", 0), |
paul@5 | 867 | 62 : ("istore_3", 0), |
paul@5 | 868 | 63 : ("lstore_0", 0), |
paul@5 | 869 | 64 : ("lstore_1", 0), |
paul@5 | 870 | 65 : ("lstore_2", 0), |
paul@5 | 871 | 66 : ("lstore_3", 0), |
paul@5 | 872 | 67 : ("fstore_0", 0), |
paul@5 | 873 | 68 : ("fstore_1", 0), |
paul@5 | 874 | 69 : ("fstore_2", 0), |
paul@5 | 875 | 70 : ("fstore_3", 0), |
paul@5 | 876 | 71 : ("dstore_0", 0), |
paul@5 | 877 | 72 : ("dstore_1", 0), |
paul@5 | 878 | 73 : ("dstore_2", 0), |
paul@5 | 879 | 74 : ("dstore_3", 0), |
paul@5 | 880 | 75 : ("astore_0", 0), |
paul@5 | 881 | 76 : ("astore_1", 0), |
paul@5 | 882 | 77 : ("astore_2", 0), |
paul@5 | 883 | 78 : ("astore_3", 0), |
paul@5 | 884 | 79 : ("iastore", 0), |
paul@5 | 885 | 80 : ("lastore", 0), |
paul@5 | 886 | 81 : ("fastore", 0), |
paul@5 | 887 | 82 : ("dastore", 0), |
paul@5 | 888 | 83 : ("aastore", 0), |
paul@5 | 889 | 84 : ("bastore", 0), |
paul@5 | 890 | 85 : ("castore", 0), |
paul@5 | 891 | 86 : ("sastore", 0), |
paul@5 | 892 | 87 : ("pop", 0), |
paul@5 | 893 | 88 : ("pop2", 0), |
paul@5 | 894 | 89 : ("dup", 0), |
paul@5 | 895 | 90 : ("dup_x1", 0), |
paul@5 | 896 | 91 : ("dup_x2", 0), |
paul@5 | 897 | 92 : ("dup2", 0), |
paul@5 | 898 | 93 : ("dup2_x1", 0), |
paul@5 | 899 | 94 : ("dup2_x2", 0), |
paul@5 | 900 | 95 : ("swap", 0), |
paul@5 | 901 | 96 : ("iadd", 0), |
paul@5 | 902 | 97 : ("ladd", 0), |
paul@5 | 903 | 98 : ("fadd", 0), |
paul@5 | 904 | 99 : ("dadd", 0), |
paul@5 | 905 | 100 : ("isub", 0), |
paul@5 | 906 | 101 : ("lsub", 0), |
paul@5 | 907 | 102 : ("fsub", 0), |
paul@5 | 908 | 103 : ("dsub", 0), |
paul@5 | 909 | 104 : ("imul", 0), |
paul@5 | 910 | 105 : ("lmul", 0), |
paul@5 | 911 | 106 : ("fmul", 0), |
paul@5 | 912 | 107 : ("dmul", 0), |
paul@5 | 913 | 108 : ("idiv", 0), |
paul@5 | 914 | 109 : ("ldiv", 0), |
paul@5 | 915 | 110 : ("fdiv", 0), |
paul@5 | 916 | 111 : ("ddiv", 0), |
paul@5 | 917 | 112 : ("irem", 0), |
paul@5 | 918 | 113 : ("lrem", 0), |
paul@5 | 919 | 114 : ("frem", 0), |
paul@5 | 920 | 115 : ("drem", 0), |
paul@5 | 921 | 116 : ("ineg", 0), |
paul@5 | 922 | 117 : ("lneg", 0), |
paul@5 | 923 | 118 : ("fneg", 0), |
paul@5 | 924 | 119 : ("dneg", 0), |
paul@5 | 925 | 120 : ("ishl", 0), |
paul@5 | 926 | 121 : ("lshl", 0), |
paul@5 | 927 | 122 : ("ishr", 0), |
paul@5 | 928 | 123 : ("lshr", 0), |
paul@5 | 929 | 124 : ("iushr", 0), |
paul@5 | 930 | 125 : ("lushr", 0), |
paul@5 | 931 | 126 : ("iand", 0), |
paul@5 | 932 | 127 : ("land", 0), |
paul@5 | 933 | 128 : ("ior", 0), |
paul@5 | 934 | 129 : ("lor", 0), |
paul@5 | 935 | 130 : ("ixor", 0), |
paul@5 | 936 | 131 : ("lxor", 0), |
paul@5 | 937 | 132 : ("iinc", 2), |
paul@5 | 938 | 133 : ("i2l", 0), |
paul@5 | 939 | 134 : ("i2f", 0), |
paul@5 | 940 | 135 : ("i2d", 0), |
paul@5 | 941 | 136 : ("l2i", 0), |
paul@5 | 942 | 137 : ("l2f", 0), |
paul@5 | 943 | 138 : ("l2d", 0), |
paul@5 | 944 | 139 : ("f2i", 0), |
paul@5 | 945 | 140 : ("f2l", 0), |
paul@5 | 946 | 141 : ("f2d", 0), |
paul@5 | 947 | 142 : ("d2i", 0), |
paul@5 | 948 | 143 : ("d2l", 0), |
paul@5 | 949 | 144 : ("d2f", 0), |
paul@5 | 950 | 145 : ("i2b", 0), |
paul@5 | 951 | 146 : ("i2c", 0), |
paul@5 | 952 | 147 : ("i2s", 0), |
paul@5 | 953 | 148 : ("lcmp", 0), |
paul@5 | 954 | 149 : ("fcmpl", 0), |
paul@5 | 955 | 150 : ("fcmpg", 0), |
paul@5 | 956 | 151 : ("dcmpl", 0), |
paul@5 | 957 | 152 : ("dcmpg", 0), |
paul@5 | 958 | 153 : ("ifeq", 2), |
paul@5 | 959 | 154 : ("ifne", 2), |
paul@5 | 960 | 155 : ("iflt", 2), |
paul@5 | 961 | 156 : ("ifge", 2), |
paul@5 | 962 | 157 : ("ifgt", 2), |
paul@5 | 963 | 158 : ("ifle", 2), |
paul@5 | 964 | 159 : ("if_icmpeq", 2), |
paul@5 | 965 | 160 : ("if_icmpne", 2), |
paul@5 | 966 | 161 : ("if_icmplt", 2), |
paul@5 | 967 | 162 : ("if_icmpge", 2), |
paul@5 | 968 | 163 : ("if_icmpgt", 2), |
paul@5 | 969 | 164 : ("if_icmple", 2), |
paul@5 | 970 | 165 : ("if_acmpeq", 2), |
paul@5 | 971 | 166 : ("if_acmpne", 2), |
paul@5 | 972 | 167 : ("goto", 2), |
paul@5 | 973 | 168 : ("jsr", 2), |
paul@5 | 974 | 169 : ("ret", 1), |
paul@5 | 975 | 170 : ("tableswitch", None), # variable number of arguments |
paul@5 | 976 | 171 : ("lookupswitch", None), # variable number of arguments |
paul@5 | 977 | 172 : ("ireturn", 0), |
paul@5 | 978 | 173 : ("lreturn", 0), |
paul@5 | 979 | 174 : ("freturn", 0), |
paul@5 | 980 | 175 : ("dreturn", 0), |
paul@5 | 981 | 176 : ("areturn", 0), |
paul@5 | 982 | 177 : ("return", 0), |
paul@5 | 983 | 178 : ("getstatic", 2), |
paul@5 | 984 | 179 : ("putstatic", 2), |
paul@5 | 985 | 180 : ("getfield", 2), |
paul@5 | 986 | 181 : ("putfield", 2), |
paul@5 | 987 | 182 : ("invokevirtual", 2), |
paul@5 | 988 | 183 : ("invokespecial", 2), |
paul@5 | 989 | 184 : ("invokestatic", 2), |
paul@5 | 990 | 185 : ("invokeinterface", 4), |
paul@5 | 991 | 187 : ("new", 2), |
paul@5 | 992 | 188 : ("newarray", 1), |
paul@5 | 993 | 189 : ("anewarray", 2), |
paul@5 | 994 | 190 : ("arraylength", 0), |
paul@5 | 995 | 191 : ("athrow", 0), |
paul@5 | 996 | 192 : ("checkcast", 2), |
paul@5 | 997 | 193 : ("instanceof", 2), |
paul@5 | 998 | 194 : ("monitorenter", 0), |
paul@5 | 999 | 195 : ("monitorexit", 0), |
paul@5 | 1000 | 196 : ("wide", None), # 3 or 5 arguments, stack changes according to modified element |
paul@5 | 1001 | 197 : ("multianewarray", 3), |
paul@5 | 1002 | 198 : ("ifnull", 2), |
paul@5 | 1003 | 199 : ("ifnonnull", 2), |
paul@5 | 1004 | 200 : ("goto_w", 4), |
paul@5 | 1005 | 201 : ("jsr_w", 4), |
paul@5 | 1006 | } |
paul@5 | 1007 | |
paul@5 | 1008 | if __name__ == "__main__": |
paul@5 | 1009 | import sys |
paul@5 | 1010 | from classfile import ClassFile |
paul@5 | 1011 | f = open(sys.argv[1]) |
paul@5 | 1012 | c = ClassFile(f.read()) |
paul@4 | 1013 | |
paul@4 | 1014 | # vim: tabstop=4 expandtab shiftwidth=4 |