1 {-# LANGUAGE TypeFamilies, StandaloneDeriving, FlexibleInstances,
2 FlexibleContexts, UndecidableInstances, RecordWildCards, OverloadedStrings,
3 TypeSynonymInstances, MultiParamTypeClasses #-}
4 -- | This module declares data type for JVM instructions, and BinaryState
5 -- instances to read/write them.
20 import Control.Applicative
22 import qualified Data.Binary as Binary
23 import qualified Data.ByteString.Lazy as B
26 import Data.BinaryState
29 -- | Immediate constant. Corresponding value will be added to base opcode.
35 deriving (Eq, Ord, Enum, Show)
37 -- | Comparation operation type. Not all CMP instructions support all operations.
45 deriving (Eq, Ord, Enum, Show)
47 -- | Format of Code method attribute.
49 codeStackSize :: Word16,
50 codeMaxLocals :: Word16,
52 codeInstructions :: [Instruction],
53 codeExceptionsN :: Word16,
54 codeExceptions :: [CodeException],
56 codeAttributes :: [AttributeInfo] }
59 -- | Exception descriptor
60 data CodeException = CodeException {
64 eCatchType :: Word16 }
67 instance BinaryState Integer CodeException where
68 put (CodeException {..}) = do
74 get = CodeException <$> get <*> get <*> get <*> get
76 instance BinaryState Integer AttributeInfo where
78 let sz = 6 + attributeLength a -- full size of AttributeInfo structure
79 liftOffset (fromIntegral sz) Binary.put a
83 instance BinaryState Integer Code where
88 forM_ codeInstructions put
90 forM_ codeExceptions put
92 forM_ codeAttributes put
98 bytes <- replicateM (fromIntegral len) get
99 let bytecode = B.pack bytes
100 code = decodeWith readInstructions 0 bytecode
102 excs <- replicateM (fromIntegral excn) get
104 attrs <- replicateM (fromIntegral nAttrs) get
105 return $ Code stackSz locals len code excn excs nAttrs attrs
107 -- | Read sequence of instructions (to end of stream)
108 readInstructions :: GetState Integer [Instruction]
109 readInstructions = do
115 next <- readInstructions
118 -- | JVM instruction set
136 | BIPUSH Word8 -- ^ 16
137 | SIPUSH Word16 -- ^ 17
139 | LDC2 Word16 -- ^ 19
140 | LDC2W Word16 -- ^ 20
141 | ILOAD Word8 -- ^ 21
142 | LLOAD Word8 -- ^ 22
143 | FLOAD Word8 -- ^ 23
144 | DLOAD Word8 -- ^ 24
145 | ALOAD Word8 -- ^ 25
146 | ILOAD_ IMM -- ^ 26, 27, 28, 29
147 | LLOAD_ IMM -- ^ 30, 31, 32, 33
148 | FLOAD_ IMM -- ^ 34, 35, 36, 37
149 | DLOAD_ IMM -- ^ 38, 39, 40, 41
150 | ALOAD_ IMM -- ^ 42, 43, 44, 45
159 | ISTORE Word8 -- ^ 54
160 | LSTORE Word8 -- ^ 55
161 | FSTORE Word8 -- ^ 56
162 | DSTORE Word8 -- ^ 57
163 | ASTORE Word8 -- ^ 58
164 | ISTORE_ IMM -- ^ 59, 60, 61, 62
165 | LSTORE_ IMM -- ^ 63, 64, 65, 66
166 | FSTORE_ IMM -- ^ 67, 68, 69, 70
167 | DSTORE_ IMM -- ^ 71, 72, 73, 74
168 | ASTORE_ IMM -- ^ 75, 76, 77, 78
222 | IINC Word8 Word8 -- ^ 132
239 | FCMP CMP -- ^ 149, 150
240 | DCMP CMP -- ^ 151, 152
241 | IF CMP -- ^ 153, 154, 155, 156, 157, 158
242 | IF_ICMP CMP Word16 -- ^ 159, 160, 161, 162, 163, 164
243 | IF_ACMP CMP Word16 -- ^ 165, 166
245 | JSR Word16 -- ^ 168
247 | TABLESWITCH Word32 Word32 Word32 [Word32] -- ^ 170
248 | LOOKUPSWITCH Word32 Word32 [(Word32, Word32)] -- ^ 171
254 | GETSTATIC Word16 -- ^ 178
255 | PUTSTATIC Word16 -- ^ 179
256 | GETFIELD Word16 -- ^ 180
257 | PUTFIELD Word16 -- ^ 181
258 | INVOKEVIRTUAL Word16 -- ^ 182
259 | INVOKESPECIAL Word16 -- ^ 183
260 | INVOKESTATIC Word16 -- ^ 184
261 | INVOKEINTERFACE Word16 Word8 -- ^ 185
262 | NEW Word16 -- ^ 187
263 | NEWARRAY Word8 -- ^ 188, see @ArrayType@
264 | ANEWARRAY Word16 -- ^ 189
265 | ARRAYLENGTH -- ^ 190
267 | CHECKCAST Word16 -- ^ 192
268 | INSTANCEOF Word16 -- ^ 193
269 | MONITORENTER -- ^ 194
270 | MONITOREXIT -- ^ 195
271 | WIDE Word8 Instruction -- ^ 196
272 | MULTINANEWARRAY Word16 Word8 -- ^ 197
273 | IFNULL Word16 -- ^ 198
274 | IFNONNULL Word16 -- ^ 199
275 | GOTO_W Word32 -- ^ 200
276 | JSR_W Word32 -- ^ 201
279 -- | JVM array type (primitive types)
289 deriving (Eq, Show, Enum)
291 -- | Parse opcode with immediate constant
292 imm :: Word8 -- ^ Base opcode
293 -> (IMM -> Instruction) -- ^ Instruction constructor
294 -> Word8 -- ^ Opcode to parse
295 -> GetState s Instruction
296 imm base constr x = return $ constr $ toEnum $ fromIntegral (x-base)
298 -- | Put opcode with immediate constant
299 putImm :: Word8 -- ^ Base opcode
300 -> IMM -- ^ Constant to add to opcode
301 -> PutState Integer ()
302 putImm base i = putByte $ base + (fromIntegral $ fromEnum i)
304 atype2byte :: ArrayType -> Word8
305 atype2byte T_BOOLEAN = 4
306 atype2byte T_CHAR = 5
307 atype2byte T_FLOAT = 6
308 atype2byte T_DOUBLE = 7
309 atype2byte T_BYTE = 8
310 atype2byte T_SHORT = 9
311 atype2byte T_INT = 10
312 atype2byte T_LONG = 11
314 byte2atype :: Word8 -> GetState s ArrayType
315 byte2atype 4 = return T_BOOLEAN
316 byte2atype 5 = return T_CHAR
317 byte2atype 6 = return T_FLOAT
318 byte2atype 7 = return T_DOUBLE
319 byte2atype 8 = return T_BYTE
320 byte2atype 9 = return T_SHORT
321 byte2atype 10 = return T_INT
322 byte2atype 11 = return T_LONG
323 byte2atype x = fail $ "Unknown array type byte: " ++ show x
325 instance BinaryState Integer ArrayType where
330 put t = putByte (atype2byte t)
332 -- | Put opcode with one argument
333 put1 :: (BinaryState Integer a)
335 -> a -- ^ First argument
336 -> PutState Integer ()
341 put2 :: (BinaryState Integer a, BinaryState Integer b)
343 -> a -- ^ First argument
344 -> b -- ^ Second argument
345 -> PutState Integer ()
351 instance BinaryState Integer Instruction where
353 put ACONST_NULL = putByte 1
354 put ICONST_M1 = putByte 2
355 put ICONST_0 = putByte 3
356 put ICONST_1 = putByte 4
357 put ICONST_2 = putByte 5
358 put ICONST_3 = putByte 6
359 put ICONST_4 = putByte 7
360 put ICONST_5 = putByte 8
361 put LCONST_0 = putByte 9
362 put LCONST_1 = putByte 10
363 put FCONST_0 = putByte 11
364 put FCONST_1 = putByte 12
365 put FCONST_2 = putByte 13
366 put DCONST_0 = putByte 14
367 put DCONST_1 = putByte 15
368 put (BIPUSH x) = put1 16 x
369 put (SIPUSH x) = put1 17 x
370 put (LDC1 x) = put1 18 x
371 put (LDC2 x) = put1 19 x
372 put (LDC2W x) = put1 20 x
373 put (ILOAD x) = put1 21 x
374 put (LLOAD x) = put1 22 x
375 put (FLOAD x) = put1 23 x
376 put (DLOAD x) = put1 24 x
377 put (ALOAD x) = put1 25 x
378 put (ILOAD_ i) = putImm 26 i
379 put (LLOAD_ i) = putImm 30 i
380 put (FLOAD_ i) = putImm 34 i
381 put (DLOAD_ i) = putImm 38 i
382 put (ALOAD_ i) = putImm 42 i
383 put IALOAD = putByte 46
384 put LALOAD = putByte 47
385 put FALOAD = putByte 48
386 put DALOAD = putByte 49
387 put AALOAD = putByte 50
388 put BALOAD = putByte 51
389 put CALOAD = putByte 52
390 put SALOAD = putByte 53
391 put (ISTORE x) = put1 54 x
392 put (LSTORE x) = put1 55 x
393 put (FSTORE x) = put1 56 x
394 put (DSTORE x) = put1 57 x
395 put (ASTORE x) = put1 58 x
396 put (ISTORE_ i) = putImm 59 i
397 put (LSTORE_ i) = putImm 63 i
398 put (FSTORE_ i) = putImm 67 i
399 put (DSTORE_ i) = putImm 71 i
400 put (ASTORE_ i) = putImm 75 i
401 put IASTORE = putByte 79
402 put LASTORE = putByte 80
403 put FASTORE = putByte 81
404 put DASTORE = putByte 82
405 put AASTORE = putByte 83
406 put BASTORE = putByte 84
407 put CASTORE = putByte 85
408 put SASTORE = putByte 86
410 put POP2 = putByte 88
412 put DUP_X1 = putByte 90
413 put DUP_X2 = putByte 91
414 put DUP2 = putByte 92
415 put DUP2_X1 = putByte 93
416 put DUP2_X2 = putByte 94
417 put SWAP = putByte 95
418 put IADD = putByte 96
419 put LADD = putByte 97
420 put FADD = putByte 98
421 put DADD = putByte 99
422 put ISUB = putByte 100
423 put LSUB = putByte 101
424 put FSUB = putByte 102
425 put DSUB = putByte 103
426 put IMUL = putByte 104
427 put LMUL = putByte 105
428 put FMUL = putByte 106
429 put DMUL = putByte 107
430 put IDIV = putByte 108
431 put LDIV = putByte 109
432 put FDIV = putByte 110
433 put DDIV = putByte 111
434 put IREM = putByte 112
435 put LREM = putByte 113
436 put FREM = putByte 114
437 put DREM = putByte 115
438 put INEG = putByte 116
439 put LNEG = putByte 117
440 put FNEG = putByte 118
441 put DNEG = putByte 119
442 put ISHL = putByte 120
443 put LSHL = putByte 121
444 put ISHR = putByte 122
445 put LSHR = putByte 123
446 put IUSHR = putByte 124
447 put LUSHR = putByte 125
448 put IAND = putByte 126
449 put LAND = putByte 127
450 put IOR = putByte 128
451 put LOR = putByte 129
452 put IXOR = putByte 130
453 put LXOR = putByte 131
454 put (IINC x y) = put2 132 x y
455 put I2L = putByte 133
456 put I2F = putByte 134
457 put I2D = putByte 135
458 put L2I = putByte 136
459 put L2F = putByte 137
460 put L2D = putByte 138
461 put F2I = putByte 139
462 put F2L = putByte 140
463 put F2D = putByte 141
464 put D2I = putByte 142
465 put D2L = putByte 143
466 put D2F = putByte 144
467 put I2B = putByte 145
468 put I2C = putByte 146
469 put I2S = putByte 147
470 put LCMP = putByte 148
471 put (FCMP C_LT) = putByte 149
472 put (FCMP C_GT) = putByte 150
473 put (FCMP c) = fail $ "No such instruction: FCMP " ++ show c
474 put (DCMP C_LT) = putByte 151
475 put (DCMP C_GT) = putByte 152
476 put (DCMP c) = fail $ "No such instruction: DCMP " ++ show c
477 put (IF c) = putByte (fromIntegral $ 153 + fromEnum c)
478 put (IF_ACMP C_EQ x) = put1 165 x
479 put (IF_ACMP C_NE x) = put1 166 x
480 put (IF_ACMP c _) = fail $ "No such instruction: IF_ACMP " ++ show c
481 put (IF_ICMP c x) = putByte (fromIntegral $ 159 + fromEnum c) >> put x
482 put GOTO = putByte 167
483 put (JSR x) = put1 168 x
484 put RET = putByte 169
485 put (TABLESWITCH def low high offs) = do
488 let pads = 4 - (offset `mod` 4)
489 replicateM (fromIntegral pads) (putByte 0)
493 put (LOOKUPSWITCH def n pairs) = do
496 let pads = 4 - (offset `mod` 4)
497 replicateM (fromIntegral pads) (putByte 0)
501 put IRETURN = putByte 172
502 put LRETURN = putByte 173
503 put FRETURN = putByte 174
504 put DRETURN = putByte 175
505 put RETURN = putByte 177
506 put (GETSTATIC x) = put1 178 x
507 put (PUTSTATIC x) = put1 179 x
508 put (GETFIELD x) = put1 180 x
509 put (PUTFIELD x) = put1 181 x
510 put (INVOKEVIRTUAL x) = put1 182 x
511 put (INVOKESPECIAL x) = put1 183 x
512 put (INVOKESTATIC x) = put1 184 x
513 put (INVOKEINTERFACE x c) = put2 185 x c >> putByte 0
514 put (NEW x) = put1 187 x
515 put (NEWARRAY x) = put1 188 x
516 put (ANEWARRAY x) = put1 189 x
517 put ARRAYLENGTH = putByte 190
518 put ATHROW = putByte 191
519 put (CHECKCAST x) = put1 192 x
520 put (INSTANCEOF x) = put1 193 x
521 put MONITORENTER = putByte 194
522 put MONITOREXIT = putByte 195
523 put (WIDE x inst) = put2 196 x inst
524 put (MULTINANEWARRAY x y) = put2 197 x y
525 put (IFNULL x) = put1 198 x
526 put (IFNONNULL x) = put1 199 x
527 put (GOTO_W x) = put1 200 x
528 put (JSR_W x) = put1 201 x
534 1 -> return ACONST_NULL
535 2 -> return ICONST_M1
543 10 -> return LCONST_1
544 11 -> return FCONST_0
545 12 -> return FCONST_1
546 13 -> return FCONST_2
547 14 -> return DCONST_0
548 15 -> return DCONST_1
625 132 -> IINC <$> get <*> get
642 149 -> return $ FCMP C_LT
643 150 -> return $ FCMP C_GT
644 151 -> return $ DCMP C_LT
645 152 -> return $ DCMP C_GT
646 165 -> IF_ACMP C_EQ <$> get
647 166 -> IF_ACMP C_NE <$> get
653 let pads = 4 - (offset `mod` 4)
654 skip (fromIntegral pads)
658 offs <- replicateM (fromIntegral $ high - low + 1) get
659 return $ TABLESWITCH def low high offs
662 let pads = 4 - (offset `mod` 4)
663 skip (fromIntegral pads)
666 pairs <- replicateM (fromIntegral n) get
667 return $ LOOKUPSWITCH def n pairs
668 172 -> return IRETURN
669 173 -> return LRETURN
670 174 -> return FRETURN
671 175 -> return DRETURN
673 178 -> GETSTATIC <$> get
674 179 -> PUTSTATIC <$> get
675 180 -> GETFIELD <$> get
676 181 -> PUTFIELD <$> get
677 182 -> INVOKEVIRTUAL <$> get
678 183 -> INVOKESPECIAL <$> get
679 184 -> INVOKESTATIC <$> get
680 185 -> (INVOKEINTERFACE <$> get <*> get) <* skip 1
682 188 -> NEWARRAY <$> get
683 189 -> ANEWARRAY <$> get
684 190 -> return ARRAYLENGTH
686 192 -> CHECKCAST <$> get
687 193 -> INSTANCEOF <$> get
688 194 -> return MONITORENTER
689 195 -> return MONITOREXIT
690 196 -> WIDE <$> get <*> get
691 197 -> MULTINANEWARRAY <$> get <*> get
692 198 -> IFNULL <$> get
693 199 -> IFNONNULL <$> get
694 200 -> GOTO_W <$> get
696 _ | inRange (59, 62) c -> imm 59 ISTORE_ c
697 | inRange (63, 66) c -> imm 63 LSTORE_ c
698 | inRange (67, 70) c -> imm 67 FSTORE_ c
699 | inRange (71, 74) c -> imm 71 DSTORE_ c
700 | inRange (75, 78) c -> imm 75 ASTORE_ c
701 | inRange (26, 29) c -> imm 26 ILOAD_ c
702 | inRange (30, 33) c -> imm 30 LLOAD_ c
703 | inRange (34, 37) c -> imm 34 FLOAD_ c
704 | inRange (38, 41) c -> imm 38 DLOAD_ c
705 | inRange (42, 45) c -> imm 42 ALOAD_ c
706 | inRange (153, 158) c -> return $ IF (toEnum $ fromIntegral $ c-153)
707 | inRange (159, 164) c -> IF_ICMP (toEnum $ fromIntegral $ c-159) <$> get
708 | otherwise -> fail $ "Unknown instruction byte code: " ++ show c
710 encodeInstructions :: [Instruction] -> B.ByteString
711 encodeInstructions code =
712 let p list = forM_ list put
713 in encodeWith p (0 :: Integer) code
715 -- | Decode Java method
716 decodeMethod :: B.ByteString -> Code
717 decodeMethod str = decodeS (0 :: Integer) str
719 -- | Encode Java method
720 encodeMethod :: Code -> B.ByteString
721 encodeMethod code = encodeS (0 :: Integer) code