1 {-# LANGUAGE FlexibleContexts, TypeFamilies, OverloadedStrings #-}
2 module JVM.Generator where
4 import Control.Monad.State as St
8 import qualified Data.Map as M
9 import qualified Data.Set as S
10 import qualified Data.ByteString.Lazy as B
16 data GState = GState {
17 generated :: [Instruction],
19 doneMethods :: [Method],
20 currentMethod :: Maybe Method}
23 emptyGState = GState {
25 currentPool = M.empty,
27 currentMethod = Nothing }
29 type Generate a = State GState a
31 appendPool :: Constant -> Pool -> (Pool, Word16)
33 let size = fromIntegral (M.size pool)
34 pool' = M.insert size c pool
37 addItem :: Constant -> Generate Word16
39 pool <- St.gets currentPool
40 case lookupPool c pool of
43 let (pool', i) = appendPool c pool
45 St.put $ st {currentPool = pool'}
48 lookupPool :: Constant -> Pool -> Maybe Word16
50 fromIntegral `fmap` findIndex (== c) (M.elems pool)
52 addNT :: Binary (Signature a) => NameType a -> Generate Word16
53 addNT (NameType name sig) = do
55 x <- addItem (CNameType name bsig)
60 addSig :: MethodSignature -> Generate Word16
61 addSig c@(MethodSignature args ret) = do
65 addToPool :: Constant -> Generate Word16
66 addToPool c@(CClass str) = do
69 addToPool c@(CField cls name) = do
70 addToPool (CClass cls)
73 addToPool c@(CMethod cls name) = do
74 addToPool (CClass cls)
77 addToPool c@(CIfaceMethod cls name) = do
78 addToPool (CClass cls)
81 addToPool c@(CString str) = do
84 addToPool c@(CNameType name sig) = do
88 addToPool c = addItem c
90 putInstruction :: Instruction -> Generate ()
91 putInstruction instr = do
93 let code = generated st
94 St.put $ st {generated = code ++ [instr]}
96 i0 :: Instruction -> Generate ()
99 i1 :: (Word16 -> Instruction) -> Constant -> Generate ()
104 i8 :: (Word8 -> Instruction) -> Constant -> Generate ()
107 i0 (fn $ fromIntegral ix)
109 startMethod :: [AccessFlag] -> B.ByteString -> MethodSignature -> Generate ()
110 startMethod flags name sig = do
111 addToPool (CString name)
114 let method = Method {
115 methodAccess = S.fromList flags,
117 methodSignature = sig,
118 methodAttrs = M.empty }
119 St.put $ st {generated = [],
120 currentMethod = Just method }
122 endMethod :: Generate ()
124 m <- St.gets currentMethod
125 code <- St.gets genCode
127 Nothing -> fail "endMethod without startMethod!"
129 let method' = method {methodAttrs = M.fromList [("Code", encodeMethod code)] }
131 St.put $ st {generated = [],
132 currentMethod = Nothing,
133 doneMethods = doneMethods st ++ [method']}
135 newMethod :: [AccessFlag] -> B.ByteString -> [ArgumentSignature] -> ReturnSignature -> Generate () -> Generate ()
136 newMethod flags name args ret gen = do
137 startMethod flags name (MethodSignature args ret)
141 genCode :: GState -> Code
143 codeStackSize = 4096,
146 codeInstructions = generated st,
150 codeAttributes = [] }
152 len = fromIntegral $ B.length $ encodeInstructions (generated st)
154 initClass :: B.ByteString -> Generate Word16
156 addToPool (CClass "java/lang/Object")
157 addToPool (CClass name)
158 addToPool (CString "Code")
160 generate :: B.ByteString -> Generate () -> Class
165 res = execState generator emptyGState
168 constantPool = currentPool res,
169 classAccess = S.fromList [ACC_PUBLIC, ACC_STATIC],
171 super = Just "java/lang/Object",
174 methods = doneMethods res,
175 classAttrs = M.empty }