module JVM.Assembler
(Instruction (..),
ArrayType (..),
+ CodeException (..),
Code (..),
IMM (..),
- CMP (..)
+ CMP (..),
+ atype2byte,
+ encodeInstructions,
+ encodeMethod,
+ decodeMethod
)
where
import Control.Monad
import Control.Applicative
import Data.Ix (inRange)
-import Data.List (intercalate)
import Data.Word
-import Data.Bits
import qualified Data.Binary as Binary
-import qualified Data.Binary.Get as Get
-import Data.Char
import qualified Data.ByteString.Lazy as B
-import Data.Array
-import qualified Data.Set as S
-import qualified Data.Map as M
import Data.BinaryState
import JVM.ClassFile
-import JVM.Types
-- | Immediate constant. Corresponding value will be added to base opcode.
data IMM =
codeExceptionsN :: Word16,
codeExceptions :: [CodeException],
codeAttrsN :: Word16,
- codeAttributes :: [AttributeInfo] }
+ codeAttributes :: Attributes File }
deriving (Eq, Show)
-- | Exception descriptor
get = CodeException <$> get <*> get <*> get <*> get
-instance BinaryState Integer AttributeInfo where
+instance BinaryState Integer Attribute where
put a = do
let sz = 6 + attributeLength a -- full size of AttributeInfo structure
liftOffset (fromIntegral sz) Binary.put a
put codeExceptionsN
forM_ codeExceptions put
put codeAttrsN
- forM_ codeAttributes put
+ forM_ (attributesList codeAttributes) put
get = do
stackSz <- get
excs <- replicateM (fromIntegral excn) get
nAttrs <- get
attrs <- replicateM (fromIntegral nAttrs) get
- return $ Code stackSz locals len code excn excs nAttrs attrs
+ return $ Code stackSz locals len code excn excs nAttrs (AP attrs)
-- | Read sequence of instructions (to end of stream)
readInstructions :: GetState Integer [Instruction]
next <- readInstructions
return (x: next)
--- | JVM instruction set
+-- | JVM instruction set. For comments, see JVM specification.
data Instruction =
NOP -- ^ 0
| ACONST_NULL -- ^ 1
| JSR_W Word32 -- ^ 201
deriving (Eq, Show)
--- ^ JVM array type (primitive types)
+-- | JVM array type (primitive types)
data ArrayType =
T_BOOLEAN -- ^ 4
| T_CHAR -- ^ 5
| T_LONG -- ^ 11
deriving (Eq, Show, Enum)
--- ^ Parse opcode with immediate constant
+-- | Parse opcode with immediate constant
imm :: Word8 -- ^ Base opcode
-> (IMM -> Instruction) -- ^ Instruction constructor
-> Word8 -- ^ Opcode to parse
-> GetState s Instruction
imm base constr x = return $ constr $ toEnum $ fromIntegral (x-base)
--- ^ Put opcode with immediate constant
+-- | Put opcode with immediate constant
putImm :: Word8 -- ^ Base opcode
-> IMM -- ^ Constant to add to opcode
-> PutState Integer ()
put t = putByte (atype2byte t)
--- ^ Put opcode with one argument
+-- | Put opcode with one argument
put1 :: (BinaryState Integer a)
=> Word8 -- ^ Opcode
-> a -- ^ First argument
| inRange (153, 158) c -> return $ IF (toEnum $ fromIntegral $ c-153)
| inRange (159, 164) c -> IF_ICMP (toEnum $ fromIntegral $ c-159) <$> get
| otherwise -> fail $ "Unknown instruction byte code: " ++ show c
+
+-- | Encode list of instructions
+encodeInstructions :: [Instruction] -> B.ByteString
+encodeInstructions code =
+ let p list = forM_ list put
+ in encodeWith p (0 :: Integer) code
+-- | Decode Java method
+decodeMethod :: B.ByteString -> Code
+decodeMethod str = decodeS (0 :: Integer) str
+
+-- | Encode Java method
+encodeMethod :: Code -> B.ByteString
+encodeMethod code = encodeS (0 :: Integer) code
+