1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE LambdaCase #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
11 import qualified Data.Map as DM
12 import Control.Monad.Writer
13 import Control.Monad.State
14 import Control.Monad.ST
20 newtype Compiler a = Compiler { unCompiler :: StateT CS (WriterT [Instr] (Either String)) a }
28 instance MonadFail Compiler where fail s = Compiler $ lift $ lift $ Left s
31 , functions :: DM.Map Int [Instr]
34 runCompiler :: Compiler a -> Either String [Instr]
35 runCompiler c = execWriterT
36 $ evalStateT (unCompiler (c >> instr [Halt] >> writeFunctions))
37 $ CS {fresh=[0..], functions=DM.empty}
39 writeFunctions :: Compiler ()
40 writeFunctions = gets (DM.elems . functions) >>= tell . concat
42 instr :: [Instr] -> Compiler a
43 instr i = tell i >> pure undefined
45 freshLabel :: Compiler Int
46 freshLabel = gets fresh >>= \(f:fs)->modify (\s->s { fresh=fs }) >> pure f
48 binop :: Instr -> Compiler a1 -> Compiler a2 -> Compiler b
49 binop i l r = l >> r >> instr [i]
51 unop :: Instr -> Compiler a -> Compiler b
52 unop i l = l >> instr [i]
54 instance Expression Compiler where
55 lit v = instr [Push $ serialise v]
70 if' p t e = freshLabel >>= \elselabel-> freshLabel >>= \endiflabel->
71 p >> instr [Brf elselabel] >>
72 t >> instr [Bra endiflabel, Lbl elselabel] >>
73 e >> instr [Lbl endiflabel]
74 bottom msg = instr [Error msg]
76 instance Function () Compiler where
78 freshLabel >>= \funlabel->
79 let g :- m = def (\()->instr [Jsr funlabel])
80 in liftFunction funlabel 0 (g ()) >> unmain m
82 instance Function (Compiler a) Compiler where
84 freshLabel >>= \funlabel->
85 let g :- m = def (\a->a >> instr [Jsr funlabel])
86 in liftFunction funlabel 1 (g (instr [Arg 0])) >> unmain m
88 instance Function (Compiler a, Compiler b) Compiler where
90 freshLabel >>= \funlabel->
91 let g :- m = def (\(a, b)->a >> b >> instr [Jsr funlabel])
92 in liftFunction funlabel 2 (g (instr [Arg 1], instr [Arg 0])) >> unmain m
94 instance Function (Compiler a, Compiler b, Compiler c) Compiler where
96 freshLabel >>= \funlabel->
97 let g :- m = def (\(a, b, c)->a >> b >> c >> instr [Jsr funlabel])
98 in liftFunction funlabel 3 (g (instr [Arg 2], instr [Arg 1], instr [Arg 0])) >> unmain m
100 instance Function (Compiler a, Compiler b, Compiler c, Compiler d) Compiler where
102 freshLabel >>= \funlabel->
103 let g :- m = def (\(a, b, c, d)->a >> b >> c >> d >> instr [Jsr funlabel])
104 in liftFunction funlabel 4 (g (instr [Arg 3], instr [Arg 2], instr [Arg 1], instr [Arg 0])) >> unmain m
106 liftFunction :: Int -> Int -> Compiler a -> Compiler ()
107 liftFunction lbl nargs body = do
108 is <- snd <$> censor (\_->[]) (listen body)
109 let instructions = Lbl lbl : is ++ [Ret nargs]
110 modify (\s->s { functions=DM.insert lbl instructions $ functions s })
114 | Add | Sub | Mul | Div | Neg
116 | Eq | Neq | Le | Ge | Leq | Geq
117 | Lbl Int | Bra Int | Brf Int
120 | Jsr Int | Ret Int | Arg Int
121 | Halt | Error String
124 data Registers = Registers
129 , gp :: DM.Map Int Int
133 interpret :: Int -> [Instr] -> Int
134 interpret memsize prog = runSTArray resultStack ! (memsize-1)
136 resultStack :: ST s (STArray s Int Int)
137 resultStack = join $ int
138 <$> newListArray (0, length prog) prog
139 <*> newArray (0, memsize-1) 0
140 <*> pure (Registers {pc=0, sp=memsize-1, mp=0, hp=0, gp=DM.empty})
142 pushh :: STArray s Int Int -> Int -> Registers -> ST s Registers
143 pushh memory value reg = do
144 writeArray memory (hp reg) value
145 pure (reg { hp = hp reg + 1} )
147 loadh :: STArray s Int Int -> Int -> Registers -> ST s Registers
148 loadh memory hptr registers = readArray memory hptr >>= flip (push memory) registers
150 push :: STArray s Int Int -> Int -> Registers -> ST s Registers
151 push memory value reg = do
152 writeArray memory (sp reg) value
153 pure (reg { sp = sp reg - 1} )
155 pop :: STArray s Int Int -> Registers -> ST s (Registers, Int)
157 v <- readArray memory (sp reg + 1)
158 pure (reg { sp = sp reg + 1}, v)
160 popn :: STArray s Int Int -> Int -> Registers -> ST s (Registers, [Int])
161 popn _ 0 reg = pure (reg, [])
162 popn memory n reg = do
163 (reg', v) <- pop memory reg
164 (reg'', vs) <- popn memory (n - 1) reg'
167 bop :: (Int -> Int -> Int) -> STArray s Int Int -> Registers -> ST s Registers
168 bop op memory reg = do
169 (reg1, r) <- pop memory reg
170 uop (flip op r) memory reg1
172 uop :: (Int -> Int) -> STArray s Int Int -> Registers -> ST s Registers
173 uop op memory reg = do
174 (reg1, r) <- pop memory reg
175 push memory (op r) reg1
177 int :: STArray s Int Instr -> STArray s Int Int -> Registers -> ST s (STArray s Int Int)
178 int program memory registers = do
179 instruction <- readArray program $ pc registers
180 let reg = registers { pc = pc registers + 1 }
183 (reg', v) <- pop memory reg
184 int program memory $ reg' { gp = DM.insert r v (gp reg')}
185 Ldr r -> push memory (DM.findWithDefault 0 r $ gp reg) reg >>= int program memory
186 Pop n -> popn memory n reg >>= int program memory . fst
187 Push v -> push memory v reg >>= int program memory
188 Add -> bop (+) memory reg >>= int program memory
189 Sub -> bop (-) memory reg >>= int program memory
190 Mul -> bop (*) memory reg >>= int program memory
191 Div -> bop div memory reg >>= int program memory
192 Neg -> uop negate memory reg >>= int program memory
193 And -> bop ((b2i .) . on (&&) i2b) memory reg >>= int program memory
194 Or -> bop ((b2i .) . on (||) i2b) memory reg >>= int program memory
195 Not -> uop (b2i . Prelude.not . i2b) memory reg >>= int program memory
196 Eq -> bop ((b2i .) . (==)) memory reg >>= int program memory
197 Neq -> bop ((b2i .) . (/=)) memory reg >>= int program memory
198 Le -> bop ((b2i .) . (<)) memory reg >>= int program memory
199 Ge -> bop ((b2i .) . (>)) memory reg >>= int program memory
200 Leq -> bop ((b2i .) . (<=)) memory reg >>= int program memory
201 Geq -> bop ((b2i .) . (>=)) memory reg >>= int program memory
202 Lbl _ -> int program memory reg
203 Bra l -> branch l program reg >>= int program memory
205 (reg', v) <- pop memory reg
206 reg'' <- if i2b v then pure reg' else branch l program reg'
207 int program memory reg''
210 >>= uncurry (foldM $ flip $ pushh memory)
211 >>= push memory (hp reg + n - 1)
212 >>= int program memory
213 Ldh n -> pop memory reg >>= \(reg', hptr)->loadh memory (hptr - n - 1) reg'
214 >>= int program memory
215 Jsr i -> push memory (pc reg) reg
216 >>= push memory (mp reg)
218 >>= \r->int program memory (r { mp = sp r})
220 (reg1, rval:omp:ra:_) <- popn memory (3+n) reg
221 reg2 <- push memory rval reg1
222 int program memory $ reg2 { pc=ra, mp=omp }
224 v <- readArray memory (mp reg + 3 + n)
225 push memory v reg >>= int program memory
227 Error msg -> fail msg
229 branch :: Int -> STArray s Int Instr -> Registers -> ST s Registers
230 branch label program reg = case pc reg of
231 -1 -> getBounds program >>= \(_, m)->branch label program $ reg { pc = m - 1}
232 _ -> readArray program (pc reg) >>= \case
233 Lbl l | label == l -> pure $ reg
234 _ -> branch label program $ reg { pc = pc reg - 1 }