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@4 | 11 | |
paul@5 | 12 | # Bytecode production classes. |
paul@5 | 13 | |
paul@5 | 14 | class BytecodeWriter: |
paul@5 | 15 | def __init__(self): |
paul@5 | 16 | self.loops = [] |
paul@5 | 17 | self.jumps = [] |
paul@5 | 18 | self.output = [] |
paul@5 | 19 | self.position = 0 |
paul@5 | 20 | |
paul@5 | 21 | # Special methods. |
paul@5 | 22 | |
paul@5 | 23 | def end_loop(self): |
paul@5 | 24 | current_loop_start = self.loops.pop() |
paul@5 | 25 | self.jump_absolute(current_loop_start) |
paul@5 | 26 | self.output[current_loop_start + 1] = self.position |
paul@5 | 27 | self.pop_block() |
paul@5 | 28 | |
paul@5 | 29 | def jump_to_next(self, status): |
paul@5 | 30 | self.jumps.push(self.position) |
paul@5 | 31 | if status: |
paul@5 | 32 | self.jump_if_true() |
paul@5 | 33 | else: |
paul@5 | 34 | self.jump_if_false() |
paul@5 | 35 | |
paul@5 | 36 | def start_next(self): |
paul@5 | 37 | current_jump_start = self.jumps.pop() |
paul@5 | 38 | self.output[current_jump_start + 1] = self.position |
paul@5 | 39 | |
paul@5 | 40 | # Normal bytecode generators. |
paul@5 | 41 | |
paul@5 | 42 | def for_iter(self): |
paul@5 | 43 | self.loops.push(self.position) |
paul@5 | 44 | self.output.append(opmap["FOR_ITER"]) |
paul@5 | 45 | self.output.append(None) # To be filled in later |
paul@5 | 46 | self.position += 2 |
paul@5 | 47 | |
paul@5 | 48 | def jump_if_false(self, offset=None): |
paul@5 | 49 | self.output.append(opmap["JUMP_IF_FALSE"]) |
paul@5 | 50 | self.output.append(offset) # May be filled in later |
paul@5 | 51 | self.position += 2 |
paul@5 | 52 | |
paul@5 | 53 | def jump_if_true(self, offset=None): |
paul@5 | 54 | self.output.append(opmap["JUMP_IF_TRUE"]) |
paul@5 | 55 | self.output.append(offset) # May be filled in later |
paul@5 | 56 | self.position += 2 |
paul@5 | 57 | |
paul@4 | 58 | # Bytecode conversion. |
paul@4 | 59 | |
paul@5 | 60 | class BytecodeReader: |
paul@5 | 61 | def __init__(self, class_file): |
paul@5 | 62 | self.class_file = class_file |
paul@5 | 63 | self.position_mapping = {} |
paul@5 | 64 | |
paul@5 | 65 | def process(self, code, program): |
paul@5 | 66 | self.java_position = 0 |
paul@5 | 67 | while self.java_position < len(code): |
paul@5 | 68 | self.position_mapping[self.java_position] = program.position |
paul@5 | 69 | bytecode = ord(code[self.java_position]) |
paul@5 | 70 | mnemonic, number_of_arguments = self.java_bytecodes[bytecode] |
paul@5 | 71 | self.process_bytecode(mnemonic, number_of_arguments) |
paul@5 | 72 | |
paul@5 | 73 | def process_bytecode(self, mnemonic, number_of_arguments): |
paul@5 | 74 | if number_of_arguments is not None: |
paul@5 | 75 | arguments = [] |
paul@5 | 76 | for j in range(0, number_of_arguments): |
paul@5 | 77 | arguments.append(ord(code[self.java_position + 1 + j])) |
paul@5 | 78 | |
paul@5 | 79 | # Call the handler. |
paul@5 | 80 | getattr(self, mnemonic)(arguments, program) |
paul@5 | 81 | else: |
paul@5 | 82 | # Call the handler. |
paul@5 | 83 | number_of_arguments = getattr(self, mnemonic)(code[self.java_position+1:], program) |
paul@5 | 84 | |
paul@5 | 85 | self.java_position = self.java_position + 1 + number_of_arguments |
paul@5 | 86 | |
paul@5 | 87 | def nop(self, arguments, program): |
paul@5 | 88 | pass |
paul@5 | 89 | |
paul@5 | 90 | def aaload(self, arguments, program): |
paul@5 | 91 | # NOTE: No type checking performed. |
paul@5 | 92 | program.binary_subscr() |
paul@5 | 93 | |
paul@5 | 94 | def aastore(self, arguments, program): |
paul@5 | 95 | # NOTE: No type checking performed. |
paul@5 | 96 | # Stack: arrayref, index, value |
paul@5 | 97 | program.rot_three() # Stack: value, arrayref, index |
paul@5 | 98 | program.store_subscr() |
paul@5 | 99 | |
paul@5 | 100 | def aconst_null(self, arguments, program): |
paul@5 | 101 | program.load_global(None) |
paul@5 | 102 | |
paul@5 | 103 | def aload(self, arguments, program): |
paul@5 | 104 | program.load_fast(arguments[0]) |
paul@5 | 105 | |
paul@5 | 106 | def aload_0(self, arguments, program): |
paul@5 | 107 | program.load_fast(0) |
paul@5 | 108 | |
paul@5 | 109 | def aload_1(self, arguments, program): |
paul@5 | 110 | program.load_fast(1) |
paul@5 | 111 | |
paul@5 | 112 | def aload_2(self, arguments, program): |
paul@5 | 113 | program.load_fast(2) |
paul@5 | 114 | |
paul@5 | 115 | def aload_3(self, arguments, program): |
paul@5 | 116 | program.load_fast(3) |
paul@5 | 117 | |
paul@5 | 118 | def anewarray(self, arguments, program): |
paul@5 | 119 | # NOTE: Does not raise NegativeArraySizeException. |
paul@5 | 120 | # NOTE: Not using the index to type the list/array. |
paul@5 | 121 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 122 | |
paul@5 | 123 | program.build_list() |
paul@5 | 124 | program.setup_loop() |
paul@5 | 125 | program.load_global("range") |
paul@5 | 126 | program.load_const(0) # Stack: list, count, range, 0 |
paul@5 | 127 | program.rot_three() # Stack: list, 0, count, range |
paul@5 | 128 | program.rot_three() # Stack: list, range, 0, count |
paul@5 | 129 | program.call_function(2) # Stack: list, range_list |
paul@5 | 130 | program.get_iter() # Stack: list, iter |
paul@5 | 131 | program.for_iter() # Stack: list, iter, value |
paul@5 | 132 | program.pop_top() # Stack: list, iter |
paul@5 | 133 | program.rot_two() # Stack: iter, list |
paul@5 | 134 | program.dup_top() # Stack: iter, list, list |
paul@5 | 135 | program.load_attr("append") # Stack: iter, list, append |
paul@5 | 136 | program.load_global(None) # Stack: iter, list, append, None |
paul@5 | 137 | program.call_function(1) # Stack: iter, list, None |
paul@5 | 138 | program.pop_top() # Stack: iter, list |
paul@5 | 139 | program.rot_two() # Stack: list, iter |
paul@5 | 140 | program.end_loop() # Back to for_iter above |
paul@5 | 141 | |
paul@5 | 142 | def areturn(self, arguments, program): |
paul@5 | 143 | program.return_value() |
paul@5 | 144 | |
paul@5 | 145 | def arraylength(self, arguments, program): |
paul@5 | 146 | program.load_global("len") # Stack: arrayref, len |
paul@5 | 147 | program.rot_two() # Stack: len, arrayref |
paul@5 | 148 | program.call_function(1) |
paul@4 | 149 | |
paul@5 | 150 | def astore(self, arguments, program): |
paul@5 | 151 | program.store_fast(arguments[0]) |
paul@5 | 152 | |
paul@5 | 153 | def astore_0(self, arguments, program): |
paul@5 | 154 | program.store_fast(0) |
paul@5 | 155 | |
paul@5 | 156 | def astore_1(self, arguments, program): |
paul@5 | 157 | program.store_fast(1) |
paul@5 | 158 | |
paul@5 | 159 | def astore_2(self, arguments, program): |
paul@5 | 160 | program.store_fast(2) |
paul@5 | 161 | |
paul@5 | 162 | def astore_3(self, arguments, program): |
paul@5 | 163 | program.store_fast(3) |
paul@5 | 164 | |
paul@5 | 165 | def athrow(self, arguments, program): |
paul@5 | 166 | # NOTE: NullPointerException not raised where null/None is found on the stack. |
paul@5 | 167 | program.raise_varargs(1) |
paul@5 | 168 | |
paul@5 | 169 | baload = aaload |
paul@5 | 170 | bastore = aastore |
paul@5 | 171 | |
paul@5 | 172 | def bipush(self, arguments, program): |
paul@5 | 173 | program.load_const(arguments[0]) |
paul@5 | 174 | |
paul@5 | 175 | caload = aaload |
paul@5 | 176 | castore = aastore |
paul@5 | 177 | |
paul@5 | 178 | def checkcast(self, arguments, program): |
paul@5 | 179 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 180 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 181 | target_components = target_name.split("/") |
paul@5 | 182 | |
paul@5 | 183 | program.dup_top() # Stack: objectref, objectref |
paul@5 | 184 | program.load_global("isinstance") # Stack: objectref, objectref, isinstance |
paul@5 | 185 | program.rot_two() # Stack: objectref, isinstance, objectref |
paul@5 | 186 | program.load_global(target_components[0]) |
paul@5 | 187 | for target_component in target_components[1:]: |
paul@5 | 188 | program.load_attr(target_component) |
paul@5 | 189 | program.call_function(2) # Stack: objectref |
paul@5 | 190 | |
paul@5 | 191 | def d2f(self, arguments, program): |
paul@5 | 192 | pass |
paul@5 | 193 | |
paul@5 | 194 | def d2i(self, arguments, program): |
paul@5 | 195 | program.load_global("int") # Stack: value, int |
paul@5 | 196 | program.rot_two() # Stack: int, value |
paul@5 | 197 | program.call_function(1) # Stack: result |
paul@5 | 198 | |
paul@5 | 199 | d2l = d2i # Preserving Java semantics |
paul@5 | 200 | |
paul@5 | 201 | def dadd(self, arguments, program): |
paul@5 | 202 | # NOTE: No type checking performed. |
paul@5 | 203 | program.binary_add() |
paul@5 | 204 | |
paul@5 | 205 | daload = aaload |
paul@5 | 206 | dastore = aastore |
paul@5 | 207 | |
paul@5 | 208 | def dcmpg(self, arguments, program): |
paul@5 | 209 | # NOTE: No type checking performed. |
paul@5 | 210 | program.compare_op(">") |
paul@5 | 211 | |
paul@5 | 212 | def dcmpl(self, arguments, program): |
paul@5 | 213 | # NOTE: No type checking performed. |
paul@5 | 214 | program.compare_op("<") |
paul@5 | 215 | |
paul@5 | 216 | def dconst_0(self, arguments, program): |
paul@5 | 217 | program.load_const(0.0) |
paul@5 | 218 | |
paul@5 | 219 | def dconst_1(self, arguments, program): |
paul@5 | 220 | program.load_const(1.0) |
paul@5 | 221 | |
paul@5 | 222 | def ddiv(self, arguments, program): |
paul@5 | 223 | # NOTE: No type checking performed. |
paul@5 | 224 | program.binary_divide() |
paul@5 | 225 | |
paul@5 | 226 | dload = aload |
paul@5 | 227 | dload_0 = aload_0 |
paul@5 | 228 | dload_1 = aload_1 |
paul@5 | 229 | dload_2 = aload_2 |
paul@5 | 230 | dload_3 = aload_3 |
paul@5 | 231 | |
paul@5 | 232 | def dmul(self, arguments, program): |
paul@5 | 233 | # NOTE: No type checking performed. |
paul@5 | 234 | program.binary_multiply() |
paul@5 | 235 | |
paul@5 | 236 | def dneg(self, arguments, program): |
paul@5 | 237 | # NOTE: No type checking performed. |
paul@5 | 238 | program.unary_negative() |
paul@4 | 239 | |
paul@5 | 240 | def drem(self, arguments, program): |
paul@5 | 241 | # NOTE: No type checking performed. |
paul@5 | 242 | program.binary_modulo() |
paul@5 | 243 | |
paul@5 | 244 | dreturn = areturn |
paul@5 | 245 | dstore = astore |
paul@5 | 246 | dstore_0 = astore_0 |
paul@5 | 247 | dstore_1 = astore_1 |
paul@5 | 248 | dstore_2 = astore_2 |
paul@5 | 249 | dstore_3 = astore_3 |
paul@5 | 250 | |
paul@5 | 251 | def dsub(self, arguments, program): |
paul@5 | 252 | # NOTE: No type checking performed. |
paul@5 | 253 | program.binary_subtract() |
paul@5 | 254 | |
paul@5 | 255 | def dup(self, arguments, program): |
paul@5 | 256 | program.dup_top() |
paul@5 | 257 | |
paul@5 | 258 | def dup_x1(self, arguments, program): |
paul@5 | 259 | # Ignoring computational type categories. |
paul@5 | 260 | program.dup_top() |
paul@5 | 261 | program.rot_three() |
paul@5 | 262 | |
paul@5 | 263 | def dup_x2(self, arguments, program): |
paul@5 | 264 | # Ignoring computational type categories. |
paul@5 | 265 | program.dup_top() |
paul@5 | 266 | program.rot_four() |
paul@5 | 267 | |
paul@5 | 268 | dup2 = dup # Ignoring computational type categories |
paul@5 | 269 | dup2_x1 = dup_x1 # Ignoring computational type categories |
paul@5 | 270 | dup2_x2 = dup_x2 # Ignoring computational type categories |
paul@5 | 271 | |
paul@5 | 272 | def f2d(self, arguments, program): |
paul@5 | 273 | pass # Preserving Java semantics |
paul@5 | 274 | |
paul@5 | 275 | def f2i(self, arguments, program): |
paul@5 | 276 | program.load_global("int") # Stack: value, int |
paul@5 | 277 | program.rot_two() # Stack: int, value |
paul@5 | 278 | program.call_function(1) # Stack: result |
paul@5 | 279 | |
paul@5 | 280 | f2l = f2i # Preserving Java semantics |
paul@5 | 281 | fadd = dadd |
paul@5 | 282 | faload = daload |
paul@5 | 283 | fastore = dastore |
paul@5 | 284 | fcmpg = dcmpg |
paul@5 | 285 | fcmpl = dcmpl |
paul@5 | 286 | fconst_0 = dconst_0 |
paul@5 | 287 | fconst_1 = dconst_1 |
paul@5 | 288 | |
paul@5 | 289 | def fconst_2(self, arguments, program): |
paul@5 | 290 | program.load_const(2.0) |
paul@5 | 291 | |
paul@5 | 292 | fdiv = ddiv |
paul@5 | 293 | fload = dload |
paul@5 | 294 | fload_0 = dload_0 |
paul@5 | 295 | fload_1 = dload_1 |
paul@5 | 296 | fload_2 = dload_2 |
paul@5 | 297 | fload_3 = dload_3 |
paul@5 | 298 | fmul = dmul |
paul@5 | 299 | fneg = dneg |
paul@5 | 300 | frem = drem |
paul@5 | 301 | freturn = dreturn |
paul@5 | 302 | fstore = dstore |
paul@5 | 303 | fstore_0 = dstore_0 |
paul@5 | 304 | fstore_1 = dstore_1 |
paul@5 | 305 | fstore_2 = dstore_2 |
paul@5 | 306 | fstore_3 = dstore_3 |
paul@5 | 307 | fsub = dsub |
paul@5 | 308 | |
paul@5 | 309 | def getfield(self, arguments, program): |
paul@5 | 310 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 311 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 312 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@5 | 313 | program.load_attr(str(target_name)) |
paul@5 | 314 | |
paul@5 | 315 | getstatic = getfield # Ignoring Java restrictions |
paul@5 | 316 | |
paul@5 | 317 | def goto(self, arguments, program): |
paul@5 | 318 | offset = arguments[0] << 8 + arguments[1] |
paul@5 | 319 | java_absolute = self.java_position + offset |
paul@5 | 320 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@5 | 321 | |
paul@5 | 322 | def goto_w(self, arguments, program): |
paul@5 | 323 | offset = arguments[0] << 24 + arguments[1] << 16 + arguments[2] << 8 + arguments[3] |
paul@5 | 324 | java_absolute = self.java_position + offset |
paul@5 | 325 | program.jump_absolute(self.position_mapping[java_absolute]) |
paul@5 | 326 | |
paul@5 | 327 | def i2b(self, arguments, program): |
paul@5 | 328 | pass |
paul@4 | 329 | |
paul@5 | 330 | def i2c(self, arguments, program): |
paul@5 | 331 | program.load_global("chr") # Stack: value, chr |
paul@5 | 332 | program.rot_two() # Stack: chr, value |
paul@5 | 333 | program.call_function(1) # Stack: result |
paul@5 | 334 | |
paul@5 | 335 | def i2d(self, arguments, program): |
paul@5 | 336 | program.load_global("float") # Stack: value, float |
paul@5 | 337 | program.rot_two() # Stack: float, value |
paul@5 | 338 | program.call_function(1) # Stack: result |
paul@5 | 339 | |
paul@5 | 340 | i2f = i2d # Not distinguishing between float and double |
paul@5 | 341 | |
paul@5 | 342 | def i2l(self, arguments, program): |
paul@5 | 343 | pass # Preserving Java semantics |
paul@5 | 344 | |
paul@5 | 345 | def i2s(self, arguments, program): |
paul@5 | 346 | pass # Not distinguishing between int and short |
paul@5 | 347 | |
paul@5 | 348 | iadd = fadd |
paul@5 | 349 | iaload = faload |
paul@5 | 350 | |
paul@5 | 351 | def iand(self, arguments, program): |
paul@5 | 352 | # NOTE: No type checking performed. |
paul@5 | 353 | program.binary_and() |
paul@5 | 354 | |
paul@5 | 355 | iastore = fastore |
paul@5 | 356 | |
paul@5 | 357 | def iconst_m1(self, arguments, program): |
paul@5 | 358 | program.load_const(-1) |
paul@5 | 359 | |
paul@5 | 360 | def iconst_0(self, arguments, program): |
paul@5 | 361 | program.load_const(0) |
paul@5 | 362 | |
paul@5 | 363 | def iconst_1(self, arguments, program): |
paul@5 | 364 | program.load_const(1) |
paul@5 | 365 | |
paul@5 | 366 | def iconst_2(self, arguments, program): |
paul@5 | 367 | program.load_const(2) |
paul@5 | 368 | |
paul@5 | 369 | def iconst_3(self, arguments, program): |
paul@5 | 370 | program.load_const(3) |
paul@5 | 371 | |
paul@5 | 372 | def iconst_4(self, arguments, program): |
paul@5 | 373 | program.load_const(4) |
paul@4 | 374 | |
paul@5 | 375 | def iconst_5(self, arguments, program): |
paul@5 | 376 | program.load_const(5) |
paul@5 | 377 | |
paul@5 | 378 | idiv = fdiv |
paul@5 | 379 | |
paul@5 | 380 | def _if_xcmpx(self, arguments, program, op): |
paul@5 | 381 | offset = arguments[0] << 8 + arguments[1] |
paul@5 | 382 | java_absolute = self.java_position + offset |
paul@5 | 383 | program.compare_op(op) |
paul@5 | 384 | program.jump_to_next(0) # skip if false |
paul@5 | 385 | program.goto(offset) |
paul@5 | 386 | program.start_next() |
paul@5 | 387 | |
paul@5 | 388 | def if_acmpeq(self, arguments, program): |
paul@5 | 389 | # NOTE: No type checking performed. |
paul@5 | 390 | self._if_xcmpx(arguments, program, "is") |
paul@5 | 391 | |
paul@5 | 392 | def if_acmpne(self, arguments, program): |
paul@5 | 393 | # NOTE: No type checking performed. |
paul@5 | 394 | self._if_xcmpx(arguments, program, "is not") |
paul@4 | 395 | |
paul@5 | 396 | def if_icmpeq(self, arguments, program): |
paul@5 | 397 | # NOTE: No type checking performed. |
paul@5 | 398 | self._if_xcmpx(arguments, program, "==") |
paul@5 | 399 | |
paul@5 | 400 | def if_icmpne(self, arguments, program): |
paul@5 | 401 | # NOTE: No type checking performed. |
paul@5 | 402 | self._if_xcmpx(arguments, program, "!=") |
paul@5 | 403 | |
paul@5 | 404 | def if_icmplt(self, arguments, program): |
paul@5 | 405 | # NOTE: No type checking performed. |
paul@5 | 406 | self._if_xcmpx(arguments, program, "<") |
paul@5 | 407 | |
paul@5 | 408 | def if_icmpge(self, arguments, program): |
paul@5 | 409 | # NOTE: No type checking performed. |
paul@5 | 410 | self._if_xcmpx(arguments, program, ">=") |
paul@5 | 411 | |
paul@5 | 412 | def if_icmpgt(self, arguments, program): |
paul@5 | 413 | # NOTE: No type checking performed. |
paul@5 | 414 | self._if_xcmpx(arguments, program, ">") |
paul@5 | 415 | |
paul@5 | 416 | def if_icmple(self, arguments, program): |
paul@5 | 417 | # NOTE: No type checking performed. |
paul@5 | 418 | self._if_xcmpx(arguments, program, "<=") |
paul@4 | 419 | |
paul@5 | 420 | def ifeq(self, arguments, program): |
paul@5 | 421 | # NOTE: No type checking performed. |
paul@5 | 422 | program.load_const(0) |
paul@5 | 423 | self._if_xcmpx(arguments, program, "==") |
paul@5 | 424 | |
paul@5 | 425 | def ifne(self, arguments, program): |
paul@5 | 426 | # NOTE: No type checking performed. |
paul@5 | 427 | program.load_const(0) |
paul@5 | 428 | self._if_xcmpx(arguments, program, "!=") |
paul@5 | 429 | |
paul@5 | 430 | def iflt(self, arguments, program): |
paul@5 | 431 | # NOTE: No type checking performed. |
paul@5 | 432 | program.load_const(0) |
paul@5 | 433 | self._if_xcmpx(arguments, program, "<") |
paul@5 | 434 | |
paul@5 | 435 | def ifge(self, arguments, program): |
paul@5 | 436 | # NOTE: No type checking performed. |
paul@5 | 437 | program.load_const(0) |
paul@5 | 438 | self._if_xcmpx(arguments, program, ">=") |
paul@5 | 439 | |
paul@5 | 440 | def ifgt(self, arguments, program): |
paul@5 | 441 | # NOTE: No type checking performed. |
paul@5 | 442 | program.load_const(0) |
paul@5 | 443 | self._if_xcmpx(arguments, program, ">") |
paul@5 | 444 | |
paul@5 | 445 | def ifle(self, arguments, program): |
paul@5 | 446 | # NOTE: No type checking performed. |
paul@5 | 447 | program.load_const(0) |
paul@5 | 448 | self._if_xcmpx(arguments, program, "<=") |
paul@5 | 449 | |
paul@5 | 450 | def ifnonnull(self, arguments, program): |
paul@5 | 451 | # NOTE: No type checking performed. |
paul@5 | 452 | program.load_const(None) |
paul@5 | 453 | self._if_xcmpx(arguments, program, "is not") |
paul@5 | 454 | |
paul@5 | 455 | def ifnull(self, arguments, program): |
paul@5 | 456 | # NOTE: No type checking performed. |
paul@5 | 457 | program.load_const(None) |
paul@5 | 458 | self._if_xcmpx(arguments, program, "is") |
paul@5 | 459 | |
paul@5 | 460 | def iinc(self, arguments, program): |
paul@5 | 461 | # NOTE: No type checking performed. |
paul@5 | 462 | program.load_fast(arguments[0]) |
paul@5 | 463 | program.load_const(arguments[1]) |
paul@5 | 464 | program.binary_add() |
paul@5 | 465 | |
paul@5 | 466 | iload = fload |
paul@5 | 467 | iload_0 = fload_0 |
paul@5 | 468 | iload_1 = fload_1 |
paul@5 | 469 | iload_2 = fload_2 |
paul@5 | 470 | iload_3 = fload_3 |
paul@5 | 471 | imul = fmul |
paul@5 | 472 | ineg = fneg |
paul@5 | 473 | |
paul@5 | 474 | def instanceof(self, arguments, program): |
paul@5 | 475 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 476 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 477 | target_components = target_name.split("/") |
paul@5 | 478 | |
paul@5 | 479 | program.load_global("isinstance") # Stack: objectref, isinstance |
paul@5 | 480 | program.rot_two() # Stack: isinstance, objectref |
paul@5 | 481 | program.load_global(target_components[0]) |
paul@5 | 482 | for target_component in target_components[1:]: |
paul@5 | 483 | program.load_attr(target_component) |
paul@5 | 484 | program.call_function(2) # Stack: result |
paul@5 | 485 | |
paul@5 | 486 | def _invoke(self, target_name, program): |
paul@5 | 487 | program.rot_two() # Stack: tuple, objectref |
paul@5 | 488 | # NOTE: Using the string version of the name which may contain incompatible characters. |
paul@5 | 489 | program.load_attr(str(target_name)) # Stack: tuple, method |
paul@5 | 490 | program.rot_two() # Stack: method, tuple |
paul@5 | 491 | program.load_global("apply") # Stack: method, tuple, apply |
paul@5 | 492 | program.rot_three() # Stack: apply, method, tuple |
paul@5 | 493 | program.call_function(2) |
paul@5 | 494 | |
paul@5 | 495 | def invokeinterface(self, arguments, program): |
paul@5 | 496 | # NOTE: This implementation does not perform the necessary checks for |
paul@5 | 497 | # NOTE: signature-based polymorphism. |
paul@5 | 498 | # NOTE: Java rules not specifically obeyed. |
paul@5 | 499 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 500 | count = arguments[2] |
paul@5 | 501 | target_name = self.class_file.constants[index - 1].get_name() |
paul@5 | 502 | # Stack: objectref, arg1, arg2, ... |
paul@5 | 503 | program.build_tuple(count) # Stack: objectref, tuple |
paul@5 | 504 | self._invoke(target_name, program) |
paul@5 | 505 | |
paul@5 | 506 | def invokespecial(self, arguments, program): |
paul@5 | 507 | # NOTE: This implementation does not perform the necessary checks for |
paul@5 | 508 | # NOTE: signature-based polymorphism. |
paul@5 | 509 | # NOTE: Java rules not specifically obeyed. |
paul@5 | 510 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 511 | target = self.class_file.constants[index - 1] |
paul@5 | 512 | target_name = target.get_name() |
paul@5 | 513 | # Get the number of parameters from the descriptor. |
paul@5 | 514 | count = len(target.get_descriptor()[0]) |
paul@5 | 515 | # Stack: objectref, arg1, arg2, ... |
paul@5 | 516 | program.build_tuple(count) # Stack: objectref, tuple |
paul@5 | 517 | self._invoke(target_name, program) |
paul@5 | 518 | |
paul@5 | 519 | def invokestatic(self, arguments, program): |
paul@5 | 520 | # NOTE: This implementation does not perform the necessary checks for |
paul@5 | 521 | # NOTE: signature-based polymorphism. |
paul@5 | 522 | # NOTE: Java rules not specifically obeyed. |
paul@5 | 523 | index = arguments[0] << 8 + arguments[1] |
paul@5 | 524 | target = self.class_file.constants[index - 1] |
paul@5 | 525 | target_name = target.get_name() |
paul@5 | 526 | # Get the number of parameters from the descriptor. |
paul@5 | 527 | count = len(target.get_descriptor()[0]) |
paul@5 | 528 | # Stack: arg1, arg2, ... |
paul@5 | 529 | program.build_tuple(count) # Stack: tuple |
paul@5 | 530 | # NOTE: Should probably use Python static methods. |
paul@5 | 531 | program.load_name("self") # Stack: tuple, self |
paul@5 | 532 | self._invoke(target_name, program) |
paul@5 | 533 | |
paul@5 | 534 | invokevirtual = invokeinterface # Ignoring Java rules |
paul@5 | 535 | |
paul@5 | 536 | def ior(self, arguments, program): |
paul@5 | 537 | # NOTE: No type checking performed. |
paul@5 | 538 | program.binary_or() |
paul@5 | 539 | |
paul@5 | 540 | irem = frem |
paul@5 | 541 | ireturn = freturn |
paul@5 | 542 | |
paul@5 | 543 | def ishl(self, arguments, program): |
paul@5 | 544 | # NOTE: No type checking performed. |
paul@5 | 545 | # NOTE: Not verified. |
paul@5 | 546 | program.binary_lshift() |
paul@5 | 547 | |
paul@5 | 548 | def ishr(self, arguments, program): |
paul@5 | 549 | # NOTE: No type checking performed. |
paul@5 | 550 | # NOTE: Not verified. |
paul@5 | 551 | program.binary_rshift() |
paul@5 | 552 | |
paul@5 | 553 | istore = fstore |
paul@5 | 554 | istore_0 = fstore_0 |
paul@5 | 555 | istore_1 = fstore_1 |
paul@5 | 556 | istore_2 = fstore_2 |
paul@5 | 557 | istore_3 = fstore_3 |
paul@5 | 558 | isub = fsub |
paul@5 | 559 | iushr = ishr # Ignoring distinctions between arithmetic and logical shifts |
paul@5 | 560 | |
paul@5 | 561 | def ishr(self, arguments, program): |
paul@5 | 562 | # NOTE: No type checking performed. |
paul@5 | 563 | program.binary_xor() |
paul@5 | 564 | |
paul@5 | 565 | def jsr(self, arguments, program): |
paul@5 | 566 | offset = arguments[0] << 8 + arguments[1] |
paul@5 | 567 | java_absolute = self.java_position + offset |
paul@5 | 568 | # NOTE: To be implemented. |
paul@5 | 569 | |
paul@5 | 570 | def wide(self, code, program): |
paul@5 | 571 | # NOTE: To be implemented. |
paul@5 | 572 | return number_of_arguments |
paul@5 | 573 | |
paul@5 | 574 | java_bytecodes = { |
paul@5 | 575 | # code : (mnemonic, number of following bytes, change in stack) |
paul@5 | 576 | 0 : ("nop", 0), |
paul@5 | 577 | 1 : ("aconst_null", 0), |
paul@5 | 578 | 2 : ("iconst_m1", 0), |
paul@5 | 579 | 3 : ("iconst_0", 0), |
paul@5 | 580 | 4 : ("iconst_1", 0), |
paul@5 | 581 | 5 : ("iconst_2", 0), |
paul@5 | 582 | 6 : ("iconst_3", 0), |
paul@5 | 583 | 7 : ("iconst_4", 0), |
paul@5 | 584 | 8 : ("iconst_5", 0), |
paul@5 | 585 | 9 : ("lconst_0", 0), |
paul@5 | 586 | 10 : ("lconst_1", 0), |
paul@5 | 587 | 11 : ("fconst_0", 0), |
paul@5 | 588 | 12 : ("fconst_1", 0), |
paul@5 | 589 | 13 : ("fconst_2", 0), |
paul@5 | 590 | 14 : ("dconst_0", 0), |
paul@5 | 591 | 15 : ("dconst_1", 0), |
paul@5 | 592 | 16 : ("bipush", 1), |
paul@5 | 593 | 17 : ("sipush", 2), |
paul@5 | 594 | 18 : ("ldc", 1), |
paul@5 | 595 | 19 : ("ldc_w", 2), |
paul@5 | 596 | 20 : ("ldc2_w", 2), |
paul@5 | 597 | 21 : ("iload", 1), |
paul@5 | 598 | 22 : ("lload", 1), |
paul@5 | 599 | 23 : ("fload", 1), |
paul@5 | 600 | 24 : ("dload", 1), |
paul@5 | 601 | 25 : ("aload", 1), |
paul@5 | 602 | 26 : ("iload_0", 0), |
paul@5 | 603 | 27 : ("iload_1", 0), |
paul@5 | 604 | 28 : ("iload_2", 0), |
paul@5 | 605 | 29 : ("iload_3", 0), |
paul@5 | 606 | 30 : ("lload_0", 0), |
paul@5 | 607 | 31 : ("lload_1", 0), |
paul@5 | 608 | 32 : ("lload_2", 0), |
paul@5 | 609 | 33 : ("lload_3", 0), |
paul@5 | 610 | 34 : ("fload_0", 0), |
paul@5 | 611 | 35 : ("fload_1", 0), |
paul@5 | 612 | 36 : ("fload_2", 0), |
paul@5 | 613 | 37 : ("fload_3", 0), |
paul@5 | 614 | 38 : ("dload_0", 0), |
paul@5 | 615 | 39 : ("dload_1", 0), |
paul@5 | 616 | 40 : ("dload_2", 0), |
paul@5 | 617 | 41 : ("dload_3", 0), |
paul@5 | 618 | 42 : ("aload_0", 0), |
paul@5 | 619 | 43 : ("aload_1", 0), |
paul@5 | 620 | 44 : ("aload_2", 0), |
paul@5 | 621 | 45 : ("aload_3", 0), |
paul@5 | 622 | 46 : ("iaload", 0), |
paul@5 | 623 | 47 : ("laload", 0), |
paul@5 | 624 | 48 : ("faload", 0), |
paul@5 | 625 | 49 : ("daload", 0), |
paul@5 | 626 | 50 : ("aaload", 0), |
paul@5 | 627 | 51 : ("baload", 0), |
paul@5 | 628 | 52 : ("caload", 0), |
paul@5 | 629 | 53 : ("saload", 0), |
paul@5 | 630 | 54 : ("istore", 1), |
paul@5 | 631 | 55 : ("lstore", 1), |
paul@5 | 632 | 56 : ("fstore", 1), |
paul@5 | 633 | 57 : ("dstore", 1), |
paul@5 | 634 | 58 : ("astore", 1), |
paul@5 | 635 | 59 : ("istore_0", 0), |
paul@5 | 636 | 60 : ("istore_1", 0), |
paul@5 | 637 | 61 : ("istore_2", 0), |
paul@5 | 638 | 62 : ("istore_3", 0), |
paul@5 | 639 | 63 : ("lstore_0", 0), |
paul@5 | 640 | 64 : ("lstore_1", 0), |
paul@5 | 641 | 65 : ("lstore_2", 0), |
paul@5 | 642 | 66 : ("lstore_3", 0), |
paul@5 | 643 | 67 : ("fstore_0", 0), |
paul@5 | 644 | 68 : ("fstore_1", 0), |
paul@5 | 645 | 69 : ("fstore_2", 0), |
paul@5 | 646 | 70 : ("fstore_3", 0), |
paul@5 | 647 | 71 : ("dstore_0", 0), |
paul@5 | 648 | 72 : ("dstore_1", 0), |
paul@5 | 649 | 73 : ("dstore_2", 0), |
paul@5 | 650 | 74 : ("dstore_3", 0), |
paul@5 | 651 | 75 : ("astore_0", 0), |
paul@5 | 652 | 76 : ("astore_1", 0), |
paul@5 | 653 | 77 : ("astore_2", 0), |
paul@5 | 654 | 78 : ("astore_3", 0), |
paul@5 | 655 | 79 : ("iastore", 0), |
paul@5 | 656 | 80 : ("lastore", 0), |
paul@5 | 657 | 81 : ("fastore", 0), |
paul@5 | 658 | 82 : ("dastore", 0), |
paul@5 | 659 | 83 : ("aastore", 0), |
paul@5 | 660 | 84 : ("bastore", 0), |
paul@5 | 661 | 85 : ("castore", 0), |
paul@5 | 662 | 86 : ("sastore", 0), |
paul@5 | 663 | 87 : ("pop", 0), |
paul@5 | 664 | 88 : ("pop2", 0), |
paul@5 | 665 | 89 : ("dup", 0), |
paul@5 | 666 | 90 : ("dup_x1", 0), |
paul@5 | 667 | 91 : ("dup_x2", 0), |
paul@5 | 668 | 92 : ("dup2", 0), |
paul@5 | 669 | 93 : ("dup2_x1", 0), |
paul@5 | 670 | 94 : ("dup2_x2", 0), |
paul@5 | 671 | 95 : ("swap", 0), |
paul@5 | 672 | 96 : ("iadd", 0), |
paul@5 | 673 | 97 : ("ladd", 0), |
paul@5 | 674 | 98 : ("fadd", 0), |
paul@5 | 675 | 99 : ("dadd", 0), |
paul@5 | 676 | 100 : ("isub", 0), |
paul@5 | 677 | 101 : ("lsub", 0), |
paul@5 | 678 | 102 : ("fsub", 0), |
paul@5 | 679 | 103 : ("dsub", 0), |
paul@5 | 680 | 104 : ("imul", 0), |
paul@5 | 681 | 105 : ("lmul", 0), |
paul@5 | 682 | 106 : ("fmul", 0), |
paul@5 | 683 | 107 : ("dmul", 0), |
paul@5 | 684 | 108 : ("idiv", 0), |
paul@5 | 685 | 109 : ("ldiv", 0), |
paul@5 | 686 | 110 : ("fdiv", 0), |
paul@5 | 687 | 111 : ("ddiv", 0), |
paul@5 | 688 | 112 : ("irem", 0), |
paul@5 | 689 | 113 : ("lrem", 0), |
paul@5 | 690 | 114 : ("frem", 0), |
paul@5 | 691 | 115 : ("drem", 0), |
paul@5 | 692 | 116 : ("ineg", 0), |
paul@5 | 693 | 117 : ("lneg", 0), |
paul@5 | 694 | 118 : ("fneg", 0), |
paul@5 | 695 | 119 : ("dneg", 0), |
paul@5 | 696 | 120 : ("ishl", 0), |
paul@5 | 697 | 121 : ("lshl", 0), |
paul@5 | 698 | 122 : ("ishr", 0), |
paul@5 | 699 | 123 : ("lshr", 0), |
paul@5 | 700 | 124 : ("iushr", 0), |
paul@5 | 701 | 125 : ("lushr", 0), |
paul@5 | 702 | 126 : ("iand", 0), |
paul@5 | 703 | 127 : ("land", 0), |
paul@5 | 704 | 128 : ("ior", 0), |
paul@5 | 705 | 129 : ("lor", 0), |
paul@5 | 706 | 130 : ("ixor", 0), |
paul@5 | 707 | 131 : ("lxor", 0), |
paul@5 | 708 | 132 : ("iinc", 2), |
paul@5 | 709 | 133 : ("i2l", 0), |
paul@5 | 710 | 134 : ("i2f", 0), |
paul@5 | 711 | 135 : ("i2d", 0), |
paul@5 | 712 | 136 : ("l2i", 0), |
paul@5 | 713 | 137 : ("l2f", 0), |
paul@5 | 714 | 138 : ("l2d", 0), |
paul@5 | 715 | 139 : ("f2i", 0), |
paul@5 | 716 | 140 : ("f2l", 0), |
paul@5 | 717 | 141 : ("f2d", 0), |
paul@5 | 718 | 142 : ("d2i", 0), |
paul@5 | 719 | 143 : ("d2l", 0), |
paul@5 | 720 | 144 : ("d2f", 0), |
paul@5 | 721 | 145 : ("i2b", 0), |
paul@5 | 722 | 146 : ("i2c", 0), |
paul@5 | 723 | 147 : ("i2s", 0), |
paul@5 | 724 | 148 : ("lcmp", 0), |
paul@5 | 725 | 149 : ("fcmpl", 0), |
paul@5 | 726 | 150 : ("fcmpg", 0), |
paul@5 | 727 | 151 : ("dcmpl", 0), |
paul@5 | 728 | 152 : ("dcmpg", 0), |
paul@5 | 729 | 153 : ("ifeq", 2), |
paul@5 | 730 | 154 : ("ifne", 2), |
paul@5 | 731 | 155 : ("iflt", 2), |
paul@5 | 732 | 156 : ("ifge", 2), |
paul@5 | 733 | 157 : ("ifgt", 2), |
paul@5 | 734 | 158 : ("ifle", 2), |
paul@5 | 735 | 159 : ("if_icmpeq", 2), |
paul@5 | 736 | 160 : ("if_icmpne", 2), |
paul@5 | 737 | 161 : ("if_icmplt", 2), |
paul@5 | 738 | 162 : ("if_icmpge", 2), |
paul@5 | 739 | 163 : ("if_icmpgt", 2), |
paul@5 | 740 | 164 : ("if_icmple", 2), |
paul@5 | 741 | 165 : ("if_acmpeq", 2), |
paul@5 | 742 | 166 : ("if_acmpne", 2), |
paul@5 | 743 | 167 : ("goto", 2), |
paul@5 | 744 | 168 : ("jsr", 2), |
paul@5 | 745 | 169 : ("ret", 1), |
paul@5 | 746 | 170 : ("tableswitch", None), # variable number of arguments |
paul@5 | 747 | 171 : ("lookupswitch", None), # variable number of arguments |
paul@5 | 748 | 172 : ("ireturn", 0), |
paul@5 | 749 | 173 : ("lreturn", 0), |
paul@5 | 750 | 174 : ("freturn", 0), |
paul@5 | 751 | 175 : ("dreturn", 0), |
paul@5 | 752 | 176 : ("areturn", 0), |
paul@5 | 753 | 177 : ("return", 0), |
paul@5 | 754 | 178 : ("getstatic", 2), |
paul@5 | 755 | 179 : ("putstatic", 2), |
paul@5 | 756 | 180 : ("getfield", 2), |
paul@5 | 757 | 181 : ("putfield", 2), |
paul@5 | 758 | 182 : ("invokevirtual", 2), |
paul@5 | 759 | 183 : ("invokespecial", 2), |
paul@5 | 760 | 184 : ("invokestatic", 2), |
paul@5 | 761 | 185 : ("invokeinterface", 4), |
paul@5 | 762 | 187 : ("new", 2), |
paul@5 | 763 | 188 : ("newarray", 1), |
paul@5 | 764 | 189 : ("anewarray", 2), |
paul@5 | 765 | 190 : ("arraylength", 0), |
paul@5 | 766 | 191 : ("athrow", 0), |
paul@5 | 767 | 192 : ("checkcast", 2), |
paul@5 | 768 | 193 : ("instanceof", 2), |
paul@5 | 769 | 194 : ("monitorenter", 0), |
paul@5 | 770 | 195 : ("monitorexit", 0), |
paul@5 | 771 | 196 : ("wide", None), # 3 or 5 arguments, stack changes according to modified element |
paul@5 | 772 | 197 : ("multianewarray", 3), |
paul@5 | 773 | 198 : ("ifnull", 2), |
paul@5 | 774 | 199 : ("ifnonnull", 2), |
paul@5 | 775 | 200 : ("goto_w", 4), |
paul@5 | 776 | 201 : ("jsr_w", 4), |
paul@5 | 777 | } |
paul@5 | 778 | |
paul@5 | 779 | if __name__ == "__main__": |
paul@5 | 780 | import sys |
paul@5 | 781 | from classfile import ClassFile |
paul@5 | 782 | f = open(sys.argv[1]) |
paul@5 | 783 | c = ClassFile(f.read()) |
paul@4 | 784 | |
paul@4 | 785 | # vim: tabstop=4 expandtab shiftwidth=4 |