diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-12-13 22:18:43 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-12-13 22:18:43 +0100 |
commit | 3fca4bf68ff8df1639416f0630452272c647cd00 (patch) | |
tree | 6f85fbacbe99611affa5ff6025da1530e40a160a /vm.hs | |
parent | 897fb17dd6a045a7056e6d6babbbb24748f698f6 (diff) |
Datas
Diffstat (limited to 'vm.hs')
-rw-r--r-- | vm.hs | 18 |
1 files changed, 10 insertions, 8 deletions
@@ -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) |