Skip to content

Commit 785a61a

Browse files
author
Louis Jenkins
committed
Began refactoring more, and ended up shortening not only the code, and adding more helpers, but even adding useful documentation
1 parent 0ee7de2 commit 785a61a

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

VirtualMachine/ByteCode.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ module VirtualMachine.ByteCode where
2525
execute :: Runtime_Environment -> IO ()
2626
execute env = head <$> readIORef (stack env) -- Take the head of the stack (current stack frame)
2727
>>= \frame -> when (debug_mode env) (debugFrame frame >>= putStrLn) -- Optional Debug
28-
>> readIORef frame >>= \f -> readIORef (program_counter . code_segment $ f) >>= \pc ->
29-
unless (fromIntegral pc >= length (byte_code . code_segment $ f)) -- While valid program_counter
30-
(getNextBC frame >>= execute' frame >> execute env) -- Execute instruction
28+
>> getPC' frame >>= \pc -> maxPC frame >>= \max_pc ->
29+
-- While valid program_counter, execute instruction
30+
unless (pc >= max_pc) (getNextBC frame >>= execute' frame >> execute env)
3131
where
3232
-- The main dispatcher logic
3333
execute' :: StackFrame -> ByteCode -> IO ()
@@ -38,12 +38,12 @@ module VirtualMachine.ByteCode where
3838
| bc >= 1 && bc <= 15 = constOp frame bc
3939
-- Push raw byte(s)
4040
| bc == 16 || bc == 17 =
41-
-- 0x10 pushes a single byte, but 0x11 pushes a short
41+
-- Special Case: 0x10 pushes a single byte, but 0x11 pushes a short
4242
(if bc == 16 then fromIntegral <$> getNextBC frame else getNextShort frame)
4343
>>= pushOp frame . fromIntegral
44-
-- Load constant pool constant
44+
-- Load from runtime constant pool
4545
| bc >= 18 && bc <= 20 =
46-
-- Only 0x12 uses only one byte, so we add a special case
46+
-- Special Case: 0x12 uses only one byte for index, while 0x13 and 0x14 use two
4747
(if bc == 18 then fromIntegral <$> getNextBC frame else getNextShort frame)
4848
>>= loadConstantPool env . fromIntegral >>= pushOp frame
4949
-- Loads
@@ -56,9 +56,10 @@ module VirtualMachine.ByteCode where
5656
| bc >= 96 && bc <= 132 = mathOp frame bc
5757
-- Conditionals
5858
| bc >= 148 && bc <= 166 = cmpOp frame bc
59-
-- Goto
60-
| bc == 167 = (program_counter . code_segment <$> readIORef frame) >>= readIORef
61-
>>= \pc -> getNextShort frame >>= \n -> readIORef frame >>= flip (writeIORef . program_counter . code_segment) (fromIntegral (fromIntegral pc + n - 1))
59+
-- Goto: The address is the offset from the current, with the offset being
60+
-- the next two instructions. Since we advance the PC 2 (+1 from reading this
61+
-- instruction), we must decrement the count by 3 to correctly obtain the target.
62+
| bc == 167 = getNextShort frame >>= \jmp -> modifyPC frame (+ (jmp - 3))
6263
-- Return
6364
| bc == 177 = return ()
6465
-- Runtime Stubs

VirtualMachine/Stack_Frame.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ module VirtualMachine.Stack_Frame where
3232
| n > 0 = (:) <$> newIORef (VReference 0) <*> createLocals (n - 1)
3333
| otherwise = error $ "Error while attempting to create locals: n=" ++ show n
3434

35+
getPC :: StackFrame -> IO (IORef Word32)
36+
getPC frame = program_counter . code_segment <$> readIORef frame
37+
38+
getPC' :: Integral a => StackFrame -> IO a
39+
getPC' frame = getPC frame >>= \f -> fromIntegral <$> readIORef f
40+
41+
setPC :: Integral a => StackFrame -> a -> IO ()
42+
setPC frame pc = getPC frame >>= flip writeIORef (fromIntegral pc)
43+
44+
modifyPC :: Integral a => StackFrame -> (a -> a) -> IO ()
45+
modifyPC frame f = (f <$> getPC' frame) >>= setPC frame >> getPC' frame >>= print
46+
47+
maxPC :: Integral a => StackFrame -> IO a
48+
maxPC frame = fromIntegral . length <$> getInstructions frame
49+
50+
getInstructions :: StackFrame -> IO Instructions
51+
getInstructions frame = byte_code . code_segment <$> readIORef frame
52+
3553
{-
3654
Pushes a value on the operand stack
3755
-}

0 commit comments

Comments
 (0)