1 #!/usr/bin/env python 2 3 """ 4 Java bytecode conversion. Specification found at the following URL: 5 http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc.html 6 """ 7 8 import dis # for access to Python bytecode values 9 10 # Bytecode conversion. 11 12 def get_instructions(code): 13 global java_bytecodes 14 15 i = 0 16 instructions = [] 17 while i < len(code): 18 bytecode = ord(code[i]) 19 mnemonic, number_of_arguments, stack_change = java_bytecodes[bytecode] 20 21 # NOTE: To be fixed. 22 if number_of_arguments is None: 23 print "Stop at", mnemonic 24 return instructions 25 26 arguments = [] 27 for j in range(0, number_of_arguments): 28 arguments.append(ord(code[i + 1 + j])) 29 30 i = i + 1 + number_of_arguments 31 instructions.append((mnemonic, arguments)) 32 33 return instructions 34 35 java_bytecodes = { 36 # code : (mnemonic, number of following bytes, change in stack) 37 0 : ("nop", 0, 0), 38 1 : ("aconst_null", 0, 1), 39 2 : ("iconst_m1", 0, 1), 40 3 : ("iconst_0", 0, 1), 41 4 : ("iconst_1", 0, 1), 42 5 : ("iconst_2", 0, 1), 43 6 : ("iconst_3", 0, 1), 44 7 : ("iconst_4", 0, 1), 45 8 : ("iconst_5", 0, 1), 46 9 : ("lconst_0", 0, 1), 47 10 : ("lconst_1", 0, 1), 48 11 : ("fconst_0", 0, 1), 49 12 : ("fconst_1", 0, 1), 50 13 : ("fconst_2", 0, 1), 51 14 : ("dconst_0", 0, 1), 52 15 : ("dconst_1", 0, 1), 53 16 : ("bipush", 1, 1), 54 17 : ("sipush", 2, 1), 55 18 : ("ldc", 1, 1), 56 19 : ("ldc_w", 2, 1), 57 20 : ("ldc2_w", 2, 1), 58 21 : ("iload", 1, 1), 59 22 : ("lload", 1, 1), 60 23 : ("fload", 1, 1), 61 24 : ("dload", 1, 1), 62 25 : ("aload", 1, 1), 63 26 : ("iload_0", 0, 1), 64 27 : ("iload_1", 0, 1), 65 28 : ("iload_2", 0, 1), 66 29 : ("iload_3", 0, 1), 67 30 : ("lload_0", 0, 1), 68 31 : ("lload_1", 0, 1), 69 32 : ("lload_2", 0, 1), 70 33 : ("lload_3", 0, 1), 71 34 : ("fload_0", 0, 1), 72 35 : ("fload_1", 0, 1), 73 36 : ("fload_2", 0, 1), 74 37 : ("fload_3", 0, 1), 75 38 : ("dload_0", 0, 1), 76 39 : ("dload_1", 0, 1), 77 40 : ("dload_2", 0, 1), 78 41 : ("dload_3", 0, 1), 79 42 : ("aload_0", 0, 1), 80 43 : ("aload_1", 0, 1), 81 44 : ("aload_2", 0, 1), 82 45 : ("aload_3", 0, 1), 83 46 : ("iaload", 0, -1), 84 47 : ("laload", 0, -1), 85 48 : ("faload", 0, -1), 86 49 : ("daload", 0, -1), 87 50 : ("aaload", 0, -1), 88 51 : ("baload", 0, -1), 89 52 : ("caload", 0, -1), 90 53 : ("saload", 0, -1), 91 54 : ("istore", 1, -1), 92 55 : ("lstore", 1, -1), 93 56 : ("fstore", 1, -1), 94 57 : ("dstore", 1, -1), 95 58 : ("astore", 1, -1), 96 59 : ("istore_0", 0, -1), 97 60 : ("istore_1", 0, -1), 98 61 : ("istore_2", 0, -1), 99 62 : ("istore_3", 0, -1), 100 63 : ("lstore_0", 0, -1), 101 64 : ("lstore_1", 0, -1), 102 65 : ("lstore_2", 0, -1), 103 66 : ("lstore_3", 0, -1), 104 67 : ("fstore_0", 0, -1), 105 68 : ("fstore_1", 0, -1), 106 69 : ("fstore_2", 0, -1), 107 70 : ("fstore_3", 0, -1), 108 71 : ("dstore_0", 0, -1), 109 72 : ("dstore_1", 0, -1), 110 73 : ("dstore_2", 0, -1), 111 74 : ("dstore_3", 0, -1), 112 75 : ("astore_0", 0, -1), 113 76 : ("astore_1", 0, -1), 114 77 : ("astore_2", 0, -1), 115 78 : ("astore_3", 0, -1), 116 79 : ("iastore", 0, -3), 117 80 : ("lastore", 0, -3), 118 81 : ("fastore", 0, -3), 119 82 : ("dastore", 0, -3), 120 83 : ("aastore", 0, -3), 121 84 : ("bastore", 0, -3), 122 85 : ("castore", 0, -3), 123 86 : ("sastore", 0, -3), 124 87 : ("pop", 0, -1), 125 88 : ("pop2", 0, None), # variable number of elements removed 126 89 : ("dup", 0, 1), 127 90 : ("dup_x1", 0, 1), 128 91 : ("dup_x2", 0, 1), 129 92 : ("dup2", 0, 2), # or 1 extra stack value 130 93 : ("dup2_x1", 0, 2), # or 1 extra stack value 131 94 : ("dup2_x2", 0, 2), # or 1 extra stack value 132 95 : ("swap", 0, 0), 133 96 : ("iadd", 0, -1), 134 97 : ("ladd", 0, -1), 135 98 : ("fadd", 0, -1), 136 99 : ("dadd", 0, -1), 137 100 : ("isub", 0, -1), 138 101 : ("lsub", 0, -1), 139 102 : ("fsub", 0, -1), 140 103 : ("dsub", 0, -1), 141 104 : ("imul", 0, -1), 142 105 : ("lmul", 0, -1), 143 106 : ("fmul", 0, -1), 144 107 : ("dmul", 0, -1), 145 108 : ("idiv", 0, -1), 146 109 : ("ldiv", 0, -1), 147 110 : ("fdiv", 0, -1), 148 111 : ("ddiv", 0, -1), 149 112 : ("irem", 0, -1), 150 113 : ("lrem", 0, -1), 151 114 : ("frem", 0, -1), 152 115 : ("drem", 0, -1), 153 116 : ("ineg", 0, 0), 154 117 : ("lneg", 0, 0), 155 118 : ("fneg", 0, 0), 156 119 : ("dneg", 0, 0), 157 120 : ("ishl", 0, -1), 158 121 : ("lshl", 0, -1), 159 122 : ("ishr", 0, -1), 160 123 : ("lshr", 0, -1), 161 124 : ("iushr", 0, -1), 162 125 : ("lushr", 0, -1), 163 126 : ("iand", 0, -1), 164 127 : ("land", 0, -1), 165 128 : ("ior", 0, -1), 166 129 : ("lor", 0, -1), 167 130 : ("ixor", 0, -1), 168 131 : ("lxor", 0, -1), 169 132 : ("iinc", 2, 0), 170 133 : ("i2l", 0, 0), 171 134 : ("i2f", 0, 0), 172 135 : ("i2d", 0, 0), 173 136 : ("l2i", 0, 0), 174 137 : ("l2f", 0, 0), 175 138 : ("l2d", 0, 0), 176 139 : ("f2i", 0, 0), 177 140 : ("f2l", 0, 0), 178 141 : ("f2d", 0, 0), 179 142 : ("d2i", 0, 0), 180 143 : ("d2l", 0, 0), 181 144 : ("d2f", 0, 0), 182 145 : ("i2b", 0, 0), 183 146 : ("i2c", 0, 0), 184 147 : ("i2s", 0, 0), 185 148 : ("lcmp", 0, -1), 186 149 : ("fcmpl", 0, -1), 187 150 : ("fcmpg", 0, -1), 188 151 : ("dcmpl", 0, -1), 189 152 : ("dcmpg", 0, -1), 190 153 : ("ifeq", 2, -1), 191 154 : ("ifne", 2, -1), 192 155 : ("iflt", 2, -1), 193 156 : ("ifge", 2, -1), 194 157 : ("ifgt", 2, -1), 195 158 : ("ifle", 2, -1), 196 159 : ("if_icmpeq", 2, -2), 197 160 : ("if_icmpne", 2, -2), 198 161 : ("if_icmplt", 2, -2), 199 162 : ("if_icmpge", 2, -2), 200 163 : ("if_icmpgt", 2, -2), 201 164 : ("if_icmple", 2, -2), 202 165 : ("if_acmpeq", 2, -2), 203 166 : ("if_acmpne", 2, -2), 204 167 : ("goto", 2, 0), 205 168 : ("jsr", 2, 1), 206 169 : ("ret", 1, 0), 207 170 : ("tableswitch", None, -1), # variable number of arguments 208 171 : ("lookupswitch", None, -1), # variable number of arguments 209 172 : ("ireturn", 0, -1), 210 173 : ("lreturn", 0, -1), 211 174 : ("freturn", 0, -1), 212 175 : ("dreturn", 0, -1), 213 176 : ("areturn", 0, -1), 214 177 : ("return", 0, 0), 215 178 : ("getstatic", 2, 1), 216 179 : ("putstatic", 2, -1), 217 180 : ("getfield", 2, 0), 218 181 : ("putfield", 2, -2), 219 182 : ("invokevirtual", 2, None), # variable number of elements removed 220 183 : ("invokespecial", 2, None), # variable number of elements removed 221 184 : ("invokestatic", 2, None), # variable number of elements removed 222 185 : ("invokeinterface", 4, None), # variable number of elements removed 223 187 : ("new", 2, 1), 224 188 : ("newarray", 1, 0), 225 189 : ("anewarray", 2, 0), 226 190 : ("arraylength", 0, 0), 227 191 : ("athrow", 0, 0), 228 192 : ("checkcast", 2, 0), 229 193 : ("instanceof", 2, 0), 230 194 : ("monitorenter", 0, -1), 231 195 : ("monitorexit", 0, -1), 232 196 : ("wide", None, None), # 3 or 5 arguments, stack changes according to modified element 233 197 : ("multianewarray", 3, None), # variable number of elements removed 234 198 : ("ifnull", 2, -1), 235 199 : ("ifnonnull", 2, -1), 236 200 : ("goto_w", 4, 0), 237 201 : ("jsr_w", 4, 1), 238 } 239 240 # vim: tabstop=4 expandtab shiftwidth=4