-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArithOps.hs
404 lines (326 loc) · 9.66 KB
/
ArithOps.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
module ArithOps where
-- Export everything
import Int
import Bits
import Array
import Monad(when)
import VMErr
import VMHeap
import VMStack
import VMMonad
import BitUtils
import {-# SOURCE #-} VM
import BasicOps(FrameOp,VMOp)
{--------------------------------------------------------------------
This module implements the interpreter functions for the
arithmetic operations defined by the JVM Specification.
They are the following:
- Addition
- Substraction
- Product
- Division
- Remainder
- Negation
- Logical and aritmetical shifting
- Logical operations (and,or,xor)
- Integer increments
- Numerical conversions (casts between primitive types)
--------------------------------------------------------------------}
--------------------------------------------------------------------
-- Addition
--------------------------------------------------------------------
iadd :: FrameOp
iadd _ _ f =
let (I v1,f1) = popOp f
(I v2,f2) = popOp f1
in (pushOp (I (v2 + v1)) f2, (+1))
ladd :: FrameOp
ladd _ _ f =
let (L v1,f1) = popOp f
(L v2,f2) = popOp f1
in (pushOp (L (v2 + v1)) f2, (+1))
fadd :: FrameOp
fadd _ _ f =
let (F v1,f1) = popOp f
(F v2,f2) = popOp f1
in (pushOp (F (v2 + v1)) f2, (+1))
dadd :: FrameOp
dadd _ _ f =
let (D v1,f1) = popOp f
(D v2,f2) = popOp f1
in (pushOp (D (v2 + v1)) f2, (+1))
--------------------------------------------------------------------
-- Substraction
--------------------------------------------------------------------
isub :: FrameOp
isub _ _ f =
let (I v1,f1) = popOp f
(I v2,f2) = popOp f1
in (pushOp (I (v2 - v1)) f2, (+1))
lsub :: FrameOp
lsub _ _ f =
let (L v1,f1) = popOp f
(L v2,f2) = popOp f1
in (pushOp (L (v2 - v1)) f2, (+1))
fsub :: FrameOp
fsub _ _ f =
let (F v1,f1) = popOp f
(F v2,f2) = popOp f1
in (pushOp (F (v2 - v1)) f2, (+1))
dsub :: FrameOp
dsub _ _ f =
let (D v1,f1) = popOp f
(D v2,f2) = popOp f1
in (pushOp (D (v2 - v1)) f2, (+1))
--------------------------------------------------------------------
-- Product
--------------------------------------------------------------------
imul :: FrameOp
imul _ _ f =
let (I v1,f1) = popOp f
(I v2,f2) = popOp f1
in (pushOp (I (v2 * v1)) f2, (+1))
lmul :: FrameOp
lmul _ _ f =
let (L v1,f1) = popOp f
(L v2,f2) = popOp f1
in (pushOp (L (v2 * v1)) f2, (+1))
fmul :: FrameOp
fmul _ _ f =
let (F v1,f1) = popOp f
(F v2,f2) = popOp f1
in (pushOp (F (v2 * v1)) f2, (+1))
dmul :: FrameOp
dmul _ _ f =
let (D v1,f1) = popOp f
(D v2,f2) = popOp f1
in (pushOp (D (v2 * v1)) f2, (+1))
--------------------------------------------------------------------
-- Division
-- Sad but true: both intger and long division throw an
-- ArithmeticException if the divisor is zero. So, these
-- operations must be monadic.
--------------------------------------------------------------------
idiv :: VMOp
idiv code pc =
do vm <- getS
let s = vmgetStack vm
let (f,s1) = popFrame s
let (I v1,f1) = popOp f
when (v1 == 0) $ raise (arithmeticException "Division by zero")
let (I v2,f2) = popOp f1
let s2 = pushFrame (pushOp (I (v2 `div` v1)) f2) s1
setS (vmsetStack vm s2)
return (+1)
ldiv :: VMOp
ldiv code pc =
do vm <- getS
let s = vmgetStack vm
let (f,s1) = popFrame s
let (L v1,f1) = popOp f
when (v1 == 0) $ raise (arithmeticException "Division by zero")
let (L v2,f2) = popOp f1
let s2 = pushFrame (pushOp (L (v2 `div` v1)) f2) s1
setS (vmsetStack vm s2)
return (+1)
fdiv :: FrameOp
fdiv _ _ f =
let (F v1,f1) = popOp f
(F v2,f2) = popOp f1
in (pushOp (F (v2 / v1)) f2, (+1))
ddiv :: FrameOp
ddiv _ _ f =
let (D v1,f1) = popOp f
(D v2,f2) = popOp f1
in (pushOp (D (v2 / v1)) f2, (+1))
--------------------------------------------------------------------
-- Remainder
--------------------------------------------------------------------
fprem :: RealFrac a => a -> a -> a
fprem n d =
let r = n/d
in n - d * fromIntegral (if r <= 0 then ceiling r else floor r)
irem :: FrameOp
irem _ _ f =
let (I v1,f1) = popOp f
(I v2,f2) = popOp f1
in (pushOp (I (v2 `mod` v1)) f2, (+1))
lrem :: FrameOp
lrem _ _ f =
let (L v1,f1) = popOp f
(L v2,f2) = popOp f1
in (pushOp (L (v2 `mod` v1)) f2, (+1))
frem :: FrameOp
frem _ _ f =
let (F v1,f1) = popOp f
(F v2,f2) = popOp f1
in (pushOp (F (v2 `fprem` v1)) f2, (+1))
drem :: FrameOp
drem _ _ f =
let (D v1,f1) = popOp f
(D v2,f2) = popOp f1
in (pushOp (D (v2 `fprem` v1)) f2, (+1))
--------------------------------------------------------------------
-- Negation
--------------------------------------------------------------------
ineg :: FrameOp
ineg _ _ f =
let (I v1,f1) = popOp f
in (pushOp (I (-v1)) f1, (+1))
lneg :: FrameOp
lneg _ _ f =
let (L v1,f1) = popOp f
in (pushOp (L (-v1)) f1, (+1))
fneg :: FrameOp
fneg _ _ f =
let (F v1,f1) = popOp f
in (pushOp (F (-v1)) f1, (+1))
dneg :: FrameOp
dneg _ _ f =
let (D v1,f1) = popOp f
in (pushOp (D (-v1)) f1, (+1))
--------------------------------------------------------------------
-- Shifting
--------------------------------------------------------------------
ishl :: FrameOp
ishl _ _ f =
let (I d,f1) = popOp f
(I i,f2) = popOp f1
in (pushOp (I (i `shiftL` (d .&. 0x1f))) f2, (+1))
lshl :: FrameOp
lshl _ _ f =
let (I d,f1) = popOp f
(L l,f2) = popOp f1
in (pushOp (L (l `shiftL` (d .&. 0x3f))) f2, (+1))
ishr :: FrameOp
ishr _ _ f =
let (I d,f1) = popOp f
(I i,f2) = popOp f1
in (pushOp (I (i `shiftR` (d .&. 0x1f))) f2, (+1))
lshr :: FrameOp
lshr _ _ f =
let (I d,f1) = popOp f
(L l,f2) = popOp f1
in (pushOp (L (l `shiftR` (d .&. 0x3f))) f2, (+1))
iushr :: FrameOp
iushr _ _ f =
let (I d,f1) = popOp f
(I i,f2) = popOp f1
v1 = i `shiftR` (d .&. 0x1f)
v2 = if i >= 0
then v1
else v1 + 2 `shiftL` (complement d .&. 0x1f)
in (pushOp (I v2) f2, (+1))
lushr :: FrameOp
lushr _ _ f =
let (I d,f1) = popOp f
(L l,f2) = popOp f1
v1 = l `shiftR` (d .&. 0x3f)
v2 = if l >= 0
then v1
else v1 + 2 `shiftL` (complement d .&. 0x3f)
in (pushOp (L v2) f2, (+1))
--------------------------------------------------------------------
-- Logical operands
--------------------------------------------------------------------
iand :: FrameOp
iand _ _ f =
let (I v1,f1) = popOp f
(I v2,f2) = popOp f1
in (pushOp (I (v2 .&. v1)) f2, (+1))
land :: FrameOp
land _ _ f =
let (L v1,f1) = popOp f
(L v2,f2) = popOp f1
in (pushOp (L (v2 .&. v1)) f2, (+1))
ior :: FrameOp
ior _ _ f =
let (I v1,f1) = popOp f
(I v2,f2) = popOp f1
in (pushOp (I (v2 .|. v1)) f2, (+1))
lor :: FrameOp
lor _ _ f =
let (L v1,f1) = popOp f
(L v2,f2) = popOp f1
in (pushOp (L (v2 .|. v1)) f2, (+1))
ixor :: FrameOp
ixor _ _ f =
let (I v1,f1) = popOp f
(I v2,f2) = popOp f1
in (pushOp (I (v2 `xor` v1)) f2, (+1))
lxor :: FrameOp
lxor _ _ f =
let (L v1,f1) = popOp f
(L v2,f2) = popOp f1
in (pushOp (L (v2 `xor` v1)) f2, (+1))
--------------------------------------------------------------------
-- Increment operation
--------------------------------------------------------------------
iinc :: FrameOp
iinc pc code f =
let ix = code ! (pc + 1)
d = sex8 (code ! (pc + 2))
I i = getVar f ix
f0 = putVar f ix (I (i + d))
in (f0, (+3))
--------------------------------------------------------------------
-- Numerical conversions
--------------------------------------------------------------------
i2l :: FrameOp
i2l _ _ f =
let (I i,f1) = popOp f
in (pushOp (L (fromIntegral i)) f1, (+1))
i2f :: FrameOp
i2f _ _ f =
let (I i,f1) = popOp f
in (pushOp (F (fromIntegral i)) f1, (+1))
i2d :: FrameOp
i2d _ _ f =
let (I i,f1) = popOp f
in (pushOp (D (fromIntegral i)) f1, (+1))
l2i :: FrameOp
l2i _ _ f =
let (L l,f1) = popOp f
in (pushOp (I (fromIntegral l)) f1, (+1))
l2f :: FrameOp
l2f _ _ f =
let (L l,f1) = popOp f
in (pushOp (F (fromIntegral l)) f1, (+1))
l2d :: FrameOp
l2d _ _ f =
let (L l,f1) = popOp f
in (pushOp (D (fromIntegral l)) f1, (+1))
f2i :: FrameOp
f2i _ _ f =
let (F _f,f1) = popOp f
in (pushOp (I (ceiling _f)) f1, (+1)) -- CHECK: Is it OK to use ceiling?
f2l :: FrameOp
f2l _ _ f =
let (F _f,f1) = popOp f
in (pushOp (L (ceiling _f)) f1, (+1)) -- CHECK: Is it OK to use ceiling?
f2d :: FrameOp
f2d _ _ f =
let (F _f,f1) = popOp f
in (pushOp (D (uncurry encodeFloat (decodeFloat _f))) f1, (+1))
d2i :: FrameOp
d2i _ _ f =
let (D d,f1) = popOp f
in (pushOp (I (ceiling d)) f1, (+1)) -- CHECK: Is it OK to use ceiling?
d2l :: FrameOp
d2l _ _ f =
let (D d,f1) = popOp f
in (pushOp (L (ceiling d)) f1, (+1)) -- CHECK: Is it OK to use ceiling?
d2f :: FrameOp
d2f _ _ f =
let (D d,f1) = popOp f
in (pushOp (F (uncurry encodeFloat (decodeFloat d))) f1, (+1))
i2b :: FrameOp
i2b _ _ f =
let (I i,f1) = popOp f
in (pushOp (I (sex8 i)) f1, (+1))
i2c :: FrameOp
i2c _ _ f =
let (I i,f1) = popOp f
in (pushOp (I (sex16 i)) f1, (+1))
i2s :: FrameOp
i2s = i2c