-
Notifications
You must be signed in to change notification settings - Fork 0
/
JThread.hs
279 lines (236 loc) · 8.67 KB
/
JThread.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
module JThread
( JThread
, run
) where
import Control.Monad.Trans
import Control.Monad.Identity
import Control.Monad.Trans.State
import Control.Monad.Trans.Error hiding (throwError)
import qualified Control.Monad.Trans.Error as E
import Control.Concurrent.MVar
import qualified Data.IntMap as IM
import Data.Maybe
import Data.Word
import Data.IntMap (IntMap)
import qualified Data.IntMap as IM
import Data.Map (Map)
import qualified Data.Map as M
import System.Directory
import ByteCode
import qualified ByteCode as BC
import ClassInfo hiding (getFieldRef, getMethodRef)
import qualified ClassInfo as CI
import JVariable
import Util
import JError
import ClassFile (ClassName)
import qualified ClassFile as CF
import JGlobal hiding (run)
import qualified JGlobal as Global
import JFrame hiding (run,popOS,pushOS,peekOS,insert,lookup)
import qualified JFrame as Frame
type JThread = StateT JThreadState (ErrorT JError IO)
data JThreadState = JThreadState
{ globalState :: MVar JGlobalState
, frames :: [JFrameState]
, currentClass :: String -- more to come
, heap :: IntMap JVariable
}
newThread gState = JThreadState
{ globalState = gState
, frames = []
, currentClass = ""
, heap = IM.empty
}
run :: JThread a -> JThreadState -> IO (Either JError (a,JThreadState))
run = (runErrorT .) . runStateT
pushFrame :: JFrameState -> JThread ()
pushFrame f = modify (\ts -> ts {frames = f : frames ts})
popFrame :: JThread ()
popFrame = modify (\ts -> ts {frames = tail (frames ts)})
execute :: ClassName -> String -> JThread ()
execute cn mn = do
inG $ initialize cn
pushFrame newFrame
callMethod cn mn
return ()
callMethod :: ClassName -> String -> JThread ()
callMethod cn mn = do
classInfo <- inG $ getClassInfo cn
method <- maybeM (ErrorMsg "no such method")
(getMethod classInfo mn)
case isNative method of
True -> nativeCall cn mn method
False -> interpreter cn 0 (method_code method)
nativeCall :: ClassName -> String -> MethodDefition -> JThread ()
nativeCall cn mn md = case (cn,mn) of
("java/io/PrintStream.class","println") -> do
VString str <- vLookup 1 -- first is this..
liftIO $ putStrLn str
popFrame
x -> throwError . strMsg $ "Native method not implemented" ++ show x
interpreter :: ClassName -> Location -> ByteCodes -> JThread ()
interpreter cn pc code = do
opcode <- maybeM (strMsg "bytecode out of bounds") $ IM.lookup pc code
case unBC opcode of
BC.ALoadC i -> next $ do
vLookup i >>= pushOS
BC.AStoreC i -> next $ popOS >>= vInsert i
BC.IConstC i -> next $ pushOS (VInteger i)
BC.IStoreC i -> next $ popOS >>= vInsert i
BC.ILoadC i -> next $ vLookup i >>= pushOS
BC.LDC i -> next $ getStringCP cn i >>= pushOS
BC.GetStatic i -> next $ getFieldRef cn i >>=
pushOS . VStaticField cn . refName
BC.InvokeVirtual i -> next $ getMethodRef cn i >>= call
BC.InvokeStatic i -> next $ getMethodRef cn i >>= callStatic
BC.InvokeSpecial i -> next $ getMethodRef cn i >>= call
BC.IAdd -> next $ math (+)
BC.ISub -> next $ math (-)
BC.IMul -> next $ math (*)
BC.IInc i c -> next $ vLookup i >>= \v -> vInsert i (v + fromIntegral c)
BC.PutField i -> next $ do
ref <- getFieldRef cn i
v <- popOS
objref <- popOS
heapUpdate objref (refName ref) v
BC.GetField i -> next $ do
classFile <- inG (getClassFile cn)
classInfo <- inG (getClassInfo cn)
ref <- getFieldRef cn i
objref <- popOS
obj <- heapLookupRef objref
pushOS $ getField obj (refName ref)
BC.Dup -> next $ peekOS >>= pushOS
BC.New i -> next $ do
className <- getClassCP cn i
obj <- new className
heapInsert obj >>= pushOS . VObjectRef
BC.IReturn -> popOS >>= \res -> popFrame >> pushOS res
BC.Return -> popFrame
BC.Goto offset -> interpreter cn (pc + offset) code
BC.If_ICmp c loc -> do
v2 <- popOS
v1 <- popOS
if icmp v1 v2 c
then interpreter cn (pc + fromIntegral loc) code
else next $ return () -- just continue
x -> throwError . strMsg $ "no such opcode: " ++ show x
where
next :: JThread a -> JThread ()
next p = p >> interpreter cn (pc + BC.sizeOfBC (fromJust $ IM.lookup pc code)) code
unBC (BC.BC _ x _ ) = x
math op = popOS >>= \v2 -> popOS >>= \v1 -> pushOS (v1 `op` v2)
icmp :: JVariable -> JVariable -> Compare -> Bool
icmp v1 v2 Eq = v1 == v2
icmp v1 v2 Ne = v1 /= v2
icmp v1 v2 Lt = v1 < v2
icmp v1 v2 Le = v1 <= v2
icmp v1 v2 Gt = v1 > v2
icmp v1 v2 Ge = v1 >= v2
new :: ClassName -> JThread JVariable
new cn = do
inG $ initialize cn
return $ VObject M.empty -- TODO: set default values
heapInsert :: JVariable -> JThread Int
heapInsert v = do
h <- gets heap
let next = 1 + IM.size h
modify (\ts -> ts { heap = IM.insert next v h} )
return next
heapLookupRef :: JVariable -> JThread JVariable
heapLookupRef (VObjectRef i) = heapLookup i
heapLookup :: Int -> JThread JVariable
heapLookup i = do
h <- gets heap
maybeM (strMsg "heapLookup") $ IM.lookup i h
heapUpdate :: JVariable -> String -> JVariable -> JThread ()
heapUpdate (VObjectRef ref) = heapUpdate' ref
heapUpdate' :: Int -> String -> JVariable -> JThread ()
heapUpdate' i field value = do
oldObj <- heapLookup i
let newObj = updateField oldObj field value
modify (\ts -> ts { heap = IM.insert i newObj (heap ts) })
updateField :: JVariable -> String -> JVariable -> JVariable
updateField (VObject fields) field v = VObject $ M.insert field v fields
getField :: JVariable -> String -> JVariable
getField (VObject fields) field = fromJust $ M.lookup field fields
-- | calls a reference method
-- | return has to pop the frame
call :: Reference -> JThread ()
call ref = do
let argumentLength = 1 + getNumberOfArgs (refType ref)
args <- replicateM argumentLength popOS
pushFrame newFrame
mapM (uncurry vInsert) $ zip [0..] (reverse args)
inG $ linking (className ref)
callMethod (className ref) (refName ref)
callStatic :: Reference -> JThread ()
callStatic ref = do
let argumentLength = getNumberOfArgs (refType ref)
args <- replicateM argumentLength popOS
pushFrame newFrame
mapM (uncurry vInsert) $ zip [0..] (reverse args)
inG $ linking (className ref)
callMethod (className ref) (refName ref)
-- | this is probably wrong, but every ; is a new argument
getNumberOfArgs :: String -> Int
getNumberOfArgs [] = 0
getNumberOfArgs ('(':xs) = getNumberOfArgs xs
getNumberOfArgs (')':xs) = 0
getNumberOfArgs ('I':xs) = 1 + getNumberOfArgs xs
getNumberOfArgs ('L':xs) = 1 + (getNumberOfArgs . tail . dropWhile (/= ';') $ xs)
getNumberOfArgs xs = error xs
--length . filter (==';')
getFieldRef :: ClassName -> Word16 -> JThread Reference
getFieldRef cn i = do
classInfo <- inG $ getClassInfo cn
maybeM (strMsg "getFieldRef") $ CI.getFieldRef classInfo (fromIntegral i)
getMethodRef :: ClassName -> Word16 -> JThread Reference
getMethodRef cn i = do
classInfo <- inG $ getClassInfo cn
maybeM (strMsg "getMethodRef") $ CI.getMethodRef classInfo (fromIntegral i)
-- get string for the constant pool
getStringCP :: ClassName -> Word8 -> JThread JVariable
getStringCP cn i = do
classFile <- inG $ getClassFile cn
return . VString $ CF.getStringCP classFile (fromIntegral i)
getClassCP :: ClassName -> Word16 -> JThread ClassName
getClassCP cn i = do
classFile <- inG (getClassFile cn)
return $ CF.getClassCP classFile (fromIntegral i)
pushOS :: JVariable -> JThread ()
pushOS = inF . Frame.pushOS
popOS :: JThread JVariable
popOS = inF Frame.popOS
peekOS :: JThread JVariable
peekOS = inF Frame.peekOS
vInsert :: Index -> JVariable -> JThread ()
vInsert = (inF .) . flip Frame.insert
vLookup :: Index -> JThread JVariable
vLookup = inF . Frame.lookup
-- util --
-- | execute inside a global
inG :: JGlobal a -> JThread a
inG p = do
gStateM <- gets globalState
gState <- liftIO $ takeMVar gStateM
(res,state) <- errorM . liftIO $ Global.run p gState
liftIO $ putMVar gStateM state
return res
-- | execute inside a jframe
inF :: JFrame a -> JThread a
inF p = do
frame <- gets (head . frames)
(res,state) <- errorM . return $ Frame.run p frame
modify (\ts -> ts {frames = state : tail (frames ts)})
return res
test = do
g <- newMVar newGlobal
liftIO $ setCurrentDirectory "example"
res <- run (execute "Main.class" "main") (newThread g)
case res of
Left err -> putStrLn . show $ err
Right v -> putStrLn "done"
throwError :: JError -> JThread a
throwError = lift . E.throwError