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