From 3fca4bf68ff8df1639416f0630452272c647cd00 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 13 Dec 2017 22:18:43 +0100 Subject: Datas --- vm.hs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'vm.hs') 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) -- cgit v1.2.3-54-g00ecf