|
1 | 1 | module VirtualMachine.ByteCode where
|
2 | 2 | import Data.IORef
|
| 3 | + import Data.Bits |
3 | 4 | import VirtualMachine.Types
|
4 | 5 | import VirtualMachine.Stack_Frame
|
5 | 6 |
|
| 7 | + execute' :: StackFrame -> ByteCode -> Instructions -> IO () |
| 8 | + execute' frame bc instrRef |
| 9 | + -- NOP |
| 10 | + | bc == 0 = return () |
| 11 | + -- *CONST* |
| 12 | + | bc >= 1 && bc <= 15 = constOp frame bc |
| 13 | + -- BIPUSH BYTE |
| 14 | + | bc == 16 = do |
| 15 | + byte <- getNextBC instrRef |
| 16 | + pushOp (fromIntegral byte) frame |
| 17 | + -- SIPUSH BYTE1 BYTE2 |
| 18 | + | bc == 17 = do |
| 19 | + byte1 <- getNextBC instrRef |
| 20 | + byte2 <- getNextBC instrRef |
| 21 | + pushOp (fromIntegral byte1 `shift` 8 .|. fromIntegral byte2) frame |
| 22 | + -- LDC* (TODO) |
| 23 | + -- *LOAD* |
| 24 | + | bc >= 21 && bc <= 53 = loadOp frame bc instrRef |
| 25 | + -- *STORE* |
| 26 | + | bc >= 54 && bc <= 86 = storeOp frame bc instrRef |
| 27 | + | otherwise = error $ "Bad ByteCode Instruction: " ++ show bc |
| 28 | + |
6 | 29 | execute :: StackFrame -> Instructions -> IO ()
|
7 | 30 | execute frame instrRef = do
|
8 |
| - mnemonic <- getNextBC instrRef |
9 |
| - case mnemonic of |
10 |
| - 1 -> pushOp 0 frame |
11 |
| - _ -> return () |
| 31 | + instr <- readIORef instrRef |
| 32 | + case length instr of |
| 33 | + 0 -> return () |
| 34 | + _ -> do |
| 35 | + bc <- getNextBC instrRef |
| 36 | + execute' frame bc instrRef |
| 37 | + execute frame instrRef |
| 38 | + |
| 39 | + |
| 40 | + constOp :: StackFrame -> ByteCode -> IO () |
| 41 | + constOp frame bc |
| 42 | + -- ACONST_NULL |
| 43 | + | bc == 1 = pushOp 0 frame |
| 44 | + -- ICONST_* |
| 45 | + | bc >= 2 && bc <= 8 = pushOp (fromIntegral $ bc - 3) frame |
| 46 | + -- LCONST_* |
| 47 | + | bc == 9 || bc == 10 = pushOp (fromIntegral $ bc - 9) frame |
| 48 | + -- FCONST_* |
| 49 | + | bc >= 11 && bc <= 13 = pushOp (fromIntegral $ bc - 11) frame |
| 50 | + -- DCONST_* |
| 51 | + | bc >= 14 && bc <= 15 = pushOp (fromIntegral $ bc - 14) frame |
| 52 | + -- |
| 53 | + | otherwise = error $ "Bad ByteCode Instruction: " ++ show bc |
| 54 | + |
| 55 | + loadOp :: StackFrame -> ByteCode -> Instructions -> IO () |
| 56 | + loadOp frame bc instrRef |
| 57 | + -- ILOAD || FLOAD || ALOAD |
| 58 | + | bc == 21 || bc == 23 || bc == 25 = do |
| 59 | + idx <- getNextBC instrRef |
| 60 | + local <- getLocal (fromIntegral idx) frame |
| 61 | + pushOp (fromIntegral local) frame |
| 62 | + -- LLOAD || DLOAD |
| 63 | + | bc == 22 || bc == 24 = do |
| 64 | + idx <- getNextBC instrRef |
| 65 | + high <- getLocal (fromIntegral idx) frame |
| 66 | + low <- getLocal (fromIntegral $ idx + 1) frame |
| 67 | + let local = high .|. low |
| 68 | + pushOp (fromIntegral local) frame |
| 69 | + -- ILOAD_* |
| 70 | + | bc >= 26 && bc <= 29 = do |
| 71 | + local <- getLocal (fromIntegral $ bc - 26) frame |
| 72 | + pushOp (fromIntegral local) frame |
| 73 | + -- LLOAD_* |
| 74 | + | bc >= 30 && bc <= 33 = do |
| 75 | + let idx = fromIntegral $ bc - 30 |
| 76 | + high <- getLocal idx frame |
| 77 | + low <- getLocal (idx + 1) frame |
| 78 | + let local = high .|. low |
| 79 | + pushOp (fromIntegral local) frame |
| 80 | + -- FLOAD_* |
| 81 | + | bc >= 34 && bc <= 37 = do |
| 82 | + local <- getLocal (fromIntegral $ bc - 34) frame |
| 83 | + pushOp (fromIntegral local) frame |
| 84 | + -- DLOAD_* |
| 85 | + | bc >= 38 && bc <= 41 = do |
| 86 | + let idx = fromIntegral $ bc - 38 |
| 87 | + high <- getLocal idx frame |
| 88 | + low <- getLocal (idx + 1) frame |
| 89 | + let local = high .|. low |
| 90 | + pushOp (fromIntegral local) frame |
| 91 | + -- ALOAD_* |
| 92 | + | bc >= 42 && bc <= 45 = do |
| 93 | + local <- getLocal (fromIntegral $ bc - 42) frame |
| 94 | + pushOp (fromIntegral local) frame |
| 95 | + -- *ALOAD (TODO) |
| 96 | + | otherwise = error $ "Bad ByteCode Instruction: " ++ show bc |
| 97 | + |
| 98 | + storeOp :: StackFrame -> ByteCode -> Instructions -> IO () |
| 99 | + storeOp frame bc instrRef |
| 100 | + -- ISTORE || FSTORE || ASTORE |
| 101 | + | bc == 54 || bc == 56 || bc == 58 = do |
| 102 | + idx <- getNextBC instrRef |
| 103 | + operand <- popOp frame |
| 104 | + putLocal (fromIntegral idx) (fromIntegral operand) frame |
| 105 | + -- LSTORE || DSTORE |
| 106 | + | bc == 55 || bc == 57 = do |
| 107 | + idx <- getNextBC instrRef |
| 108 | + operand <- popOp frame |
| 109 | + let high = operand `shiftR` 32 |
| 110 | + let low = operand .&. 0xFFFFFFFF |
| 111 | + putLocal (fromIntegral idx) (fromIntegral high) frame |
| 112 | + putLocal (fromIntegral $ idx + 1) (fromIntegral low) frame |
| 113 | + -- ISTORE_* |
| 114 | + | bc >= 59 && bc <= 62 = do |
| 115 | + operand <- popOp frame |
| 116 | + putLocal (fromIntegral $ bc - 59) (fromIntegral operand) frame |
| 117 | + -- LSTORE_* |
| 118 | + | bc >= 63 && bc <= 66 = do |
| 119 | + let idx = fromIntegral $ bc - 63 |
| 120 | + operand <- popOp frame |
| 121 | + let high = operand `shiftR` 32 |
| 122 | + let low = operand .&. 0xFFFFFFFF |
| 123 | + putLocal (fromIntegral idx) (fromIntegral high) frame |
| 124 | + putLocal (fromIntegral $ idx + 1) (fromIntegral low) frame |
| 125 | + -- FSTORE_* |
| 126 | + | bc >= 67 && bc <= 70 = do |
| 127 | + operand <- popOp frame |
| 128 | + putLocal (fromIntegral $ bc - 67) (fromIntegral operand) frame |
| 129 | + -- DSTORE_* |
| 130 | + | bc >= 71 && bc <= 74 = do |
| 131 | + let idx = fromIntegral $ bc - 71 |
| 132 | + operand <- popOp frame |
| 133 | + let high = operand `shiftR` 32 |
| 134 | + let low = operand .&. 0xFFFFFFFF |
| 135 | + putLocal (fromIntegral idx) (fromIntegral high) frame |
| 136 | + putLocal (fromIntegral $ idx + 1) (fromIntegral low) frame |
| 137 | + -- ALOAD_* |
| 138 | + | bc >= 75 && bc <= 78 = do |
| 139 | + operand <- popOp frame |
| 140 | + putLocal (fromIntegral $ bc - 75) (fromIntegral operand) frame |
| 141 | + -- *ALOAD (TODO) |
| 142 | + | otherwise = error $ "Bad ByteCode Instruction: " ++ show bc |
12 | 143 |
|
13 | 144 | getNextBC :: Instructions -> IO ByteCode
|
14 | 145 | getNextBC instrRef = do
|
|
0 commit comments