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