summaryrefslogtreecommitdiff
path: root/vm.hs
diff options
context:
space:
mode:
Diffstat (limited to 'vm.hs')
-rw-r--r--vm.hs18
1 files changed, 10 insertions, 8 deletions
diff --git a/vm.hs b/vm.hs
index 10c084f..c21e228 100644
--- a/vm.hs
+++ b/vm.hs
@@ -12,11 +12,14 @@ import AST
import Intermediate
-data Info = Info (Map.Map Int BB) (Map.Map Name GlobFuncDef)
+data Info =
+ Info (Map.Map Int BB) -- basic blocks
+ (Map.Map Name GlobFuncDef) -- global functions
+ [Value] -- data table
type TempMap = Map.Map Int RunValue
-data State = State TempMap ([RunValue], [RunValue])
+data State = State TempMap ([RunValue] {- current arguments -}, [RunValue] {- current closure -})
data RunValue = RClosure Name [RunValue] | RValue Value
deriving Show
@@ -25,12 +28,11 @@ kErrorExit :: String
kErrorExit = "VM:exit"
vmRun :: IRProgram -> IO ()
-vmRun (IRProgram bbs gfds []) =
+vmRun (IRProgram bbs gfds datas) =
let bbmap = Map.fromList [(bidOf bb, bb) | bb <- bbs]
- info = Info bbmap gfds
+ info = Info bbmap gfds datas
state = State Map.empty ([], [])
in IO.catchIOError (void $ vmRunBB info state (head bbs)) vmErrorHandler
-vmRun _ = undefined
vmErrorHandler :: IOError -> IO ()
vmErrorHandler e =
@@ -43,11 +45,11 @@ vmRunBB info state (BB _ inss term) = do
vmRunInstr :: Info -> State -> Instruction -> IO State
-- vmRunInstr _ _ ins | traceShow ins False = undefined
-vmRunInstr info@(Info bbmap gfds) state@(State tmap (args, closure)) (dest, instr) = case instr of
+vmRunInstr info@(Info bbmap gfds datas) state@(State tmap (args, closure)) (dest, instr) = case instr of
IAssign ref -> return (assignRef state dest (findRef tmap ref))
IParam i -> return (assignRef state dest (args !! i))
IClosure i -> return (assignRef state dest (closure !! i))
- IData _ -> undefined
+ IData i -> return (assignRef state dest (RValue (datas !! i)))
ICallC cl as -> case findRef tmap cl of
RClosure clname clvals -> case Map.lookup clname gfds of
Just (GlobFuncDef b _ _) ->
@@ -62,7 +64,7 @@ vmRunInstr info@(Info bbmap gfds) state@(State tmap (args, closure)) (dest, inst
IDiscard _ -> return state
vmRunTerm :: Info -> State -> Terminator -> IO (RunValue, State)
-vmRunTerm info@(Info bbmap gfds) state@(State tmap (args, closure)) term = case term of
+vmRunTerm info@(Info bbmap _ _) state@(State tmap _) term = case term of
IBr ref b1 b2 -> vmRunBB info state . (bbmap !) $ if truthy (findRef tmap ref) then b1 else b2
IJmp b -> vmRunBB info state (bbmap ! b)
IRet ref -> return (findRef tmap ref, state)