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