Sem now prints Gamma
[cc1516.git] / gen.icl
1 implementation module gen
2
3 import StdMisc
4 import StdList
5 import StdOverloaded
6 import StdString
7 from StdFunc import id, const
8 import StdTuple
9 import StdEnum
10
11 import Data.Func
12 import qualified Data.Map as Map
13 import Data.List
14 import Data.Either
15 import Data.Tuple
16 import Data.Functor
17 import Data.Monoid
18 import Data.Maybe
19 import Control.Applicative
20 import Control.Monad
21 import Control.Monad.Trans
22 from Text import class Text(concat), instance Text String
23
24 import AST
25 import RWST
26
27 TRUE :== -1
28 FALSE :== 0
29 :: Instr = Instr String [Arg] String
30 | Lab Label
31 :: Label :== String
32 :: Arg = L Label | Lit Int | Raw String
33 :: SSMProgram :== [Instr]
34 :: GenError = Error String
35 :: Addressbook :== 'Map'.Map String Address
36 :: Address = LAB String Int Int | ADDR Int Int
37 :: Gen a :== RWST () SSMProgram (Addressbook, [Label]) (Either GenError) a
38
39 labelStream :: [Label]
40 labelStream = ["lbl_" +++ toString i\\i<-[1..]]
41
42 defaultAddressBook :: [FunDecl] -> Addressbook
43 defaultAddressBook fd = extend "1printint" (LAB "1printint" 1 0)
44 $ extend "1printchar" (LAB "1printchar" 1 1)
45 $ extend "read" (LAB "read" 0 2)
46 $ extend "1readint" (LAB "1readint" 0 3)
47 $ extend "isEmpty" (LAB "isempty" 1 4)
48 $ addFuncs fd 5
49 where
50 addFuncs [] _ = 'Map'.newMap
51 addFuncs [(FunDecl _ k args _ _ _):xs] n =
52 extend k (LAB k (length args) n) $ addFuncs xs (n+1)
53
54 gen :: AST -> Either String String
55 gen (AST fds) = case evalRWST prog () (defaultAddressBook fds, labelStream) of
56 Left (Error e) = Left e
57 Right (_, p) = Right $ toString p
58 where
59 prog = tell [
60 Instr "bsr" [L "main"] "",
61 Instr "halt" [] ""
62 ] >>| tell (programContext fds)
63 >>| mapM_ g fds
64
65 programContext :: [FunDecl] -> SSMProgram
66 programContext x = [Lab "1func"
67 :fS ["1printint" ,"1printchar"
68 ,"read" ,"1readint"
69 ,"isEmpty":map (\(FunDecl _ k _ _ _ _)->k) x] 0] ++ context
70 where
71
72 fS :: [String] Int -> SSMProgram
73 fS [] _ = []
74 fS [k:xs] n = [
75 Lab $ "1next" +++ toString n
76 ,Instr "ldr" [Raw "R5"] ""
77 ,Instr "ldc" [Lit n] $ "branch to: " +++ k
78 ,Instr "eq" [] ""
79 ,Instr "brf" [L $ "1next" +++ (toString $ n + 1)] ""
80 ,Instr "bra" [L k] ""
81 :fS xs $ n+1]
82 context :: SSMProgram
83 context = [Lab "1printint"
84 ,Instr "link" [Lit 0] ""
85 ,Instr "ldl" [Lit -2] "load first argument"
86 ,Instr "trap" [Lit 0] "print int"
87 ,Instr "unlink" [] ""
88 ,Instr "ret" [] ""
89 ,Lab "1printchar"
90 ,Instr "link" [Lit 0] ""
91 ,Instr "ldl" [Lit -2] "load first argument"
92 ,Instr "trap" [Lit 1] "print char"
93 ,Instr "unlink" [] ""
94 ,Instr "ret" [] ""
95 ,Lab "read"
96 ,Instr "link" [Lit 0] ""
97 ,Instr "trap" [Lit 11] "read char"
98 ,Instr "str" [Raw "RR"] ""
99 ,Instr "unlink" [] ""
100 ,Instr "ret" [] ""
101 ,Lab "isempty"
102 ,Instr "link" [Lit 0] ""
103 ,Instr "ldl" [Lit -2] "load prt to list"
104 ,Instr "lda" [Lit 0] "derefrence ptr"
105 ,Instr "ldc" [Lit 0] ""
106 ,Instr "eq" [] "test for null pointer"
107 ,Instr "str" [Raw "RR"] ""
108 ,Instr "unlink" [] ""
109 ,Instr "ret" [] ""
110 ,Lab "read"
111 ]
112
113 getAdressbook :: Gen Addressbook
114 getAdressbook = gets fst
115
116 updateAdressbook :: (Addressbook -> Addressbook) -> Gen Addressbook
117 updateAdressbook f = modify (appFst f) >>| getAdressbook
118
119 extend :: String Address Addressbook -> Addressbook
120 extend k pl g = 'Map'.put k pl g
121
122 fresh :: Gen Label
123 fresh = gets snd >>= \vars->
124 modify (appSnd $ const $ tail vars) >>|
125 pure (head vars)
126
127 class g a :: a -> Gen ()
128
129 instance g Op1 where
130 g UnNegation = tell [Instr "not" [] ""]
131 g UnMinus = tell [Instr "neg" [] ""]
132
133 instance g Op2 where
134 g o = tell [Instr s [] ""]
135 where
136 s = case o of
137 BiPlus = "add"
138 BiMinus = "sub"
139 BiTimes = "mul"
140 BiDivide = "div"
141 BiMod = "mod"
142 BiEquals = "eq"
143 BiLesser = "lt"
144 BiGreater = "gt"
145 BiLesserEq = "le"
146 BiGreaterEq = "ge"
147 BiUnEqual = "ne"
148 BiAnd = "and"
149 BiOr = "or"
150 BiCons = abort "Shit, Cons, how to deal with this?"
151
152 instance g FieldSelector where
153 g FieldFst = tell [Instr "lda" [Lit -1] "fst"]
154 g FieldSnd = tell [Instr "lda" [Lit 0] "snd"]
155 g FieldHd = tell [Instr "lda" [Lit -1] "hd"]
156 g FieldTl = tell [Instr "lda" [Lit 0] "tl"]
157
158 instance g Expr where
159 g (IntExpr _ i) = tell [Instr "ldc" [Lit i] ""]
160 g (CharExpr _ c) = tell [Instr "ldc" [Lit (toInt c)] ""]
161 g (BoolExpr _ b) = tell [Instr "ldc" [Lit (if b TRUE FALSE)] ""]
162 g (EmptyListExpr _) = tell [Instr "ldc" [Lit 0] ""]
163 >>| tell [Instr "sth" [] ""]
164 g (Op1Expr _ o e) = g e >>| g o
165 g (Op2Expr _ e1 BiCons e2) = g e2 >>| g e1
166 >>| tell [Instr "sth" [] ""]
167 >>| tell [Instr "ajs" [Lit -1] ""]
168 >>| tell [Instr "sth" [] ""]
169 g (Op2Expr _ e1 op e2) = g e1 >>| g e2 >>| g op
170 g (TupleExpr _ (e1,e2)) = g e1
171 >>| g e2
172 >>| tell [Instr "stmh" [Lit 2] ""]
173 g (VarExpr _ (VarDef k fs)) = getAdressbook >>= \ab->case 'Map'.get k ab of
174 Just (ADDR t arity) = tell [Instr "ldl" [Lit t] ""] >>| mapM_ g fs >>| pure ()
175 Just (LAB l _ fn) = tell
176 [Instr "ldc" [Lit fn] ""
177 ,Instr "sth" [] ""
178 ,Instr "ldc" [Lit 0] ""
179 ,Instr "sth" [] ""
180 ,Instr "ajs" [Lit -1] ""]
181 g (FunExpr _ k es fs) = getAdressbook >>= \ab->case 'Map'.get k ab of
182 //Identifier points to function
183 Just (LAB l arity fn) = if (arity <> (length es))
184 //Function is not complete
185 ( tell
186 [Instr "ldc" [Lit fn] "Store function number"
187 ,Instr "sth" [] ""
188 ,Instr "ldc" [Lit $ length es] "Store arity"
189 ,Instr "sth" [] ""
190 ,Instr "ajs" [Lit -1] ""]
191 >>| mapM_ g es
192 >>| if (isEmpty es) (pure ()) (tell
193 [Instr "stmh" [Lit $ length es] "Store arguments"
194 ,Instr "ajs" [Lit -1] ""]))
195 //Function is complete
196 ( mapM_ g es
197 >>| jump "bsr" k
198 >>| tell
199 [Instr "ajs" [Lit $ ~(length es)] "Clean arguments"
200 ,Instr "ldr" [Raw "RR"] ""])
201 //Identifier points to variable, thus higher order function
202 Just (ADDR t arity) = if (arity <> (length es))
203 //Function is still not complete
204 ( fresh >>= \finish->fresh >>= \start->tell [
205 //Store function number
206 Instr "ldl" [Lit t] "STARTING HIGHER ORDER UPDATE"
207 ,Instr "ldh" [Lit 0] "get function number"
208 ,Instr "sth" [] "Store"
209 //Store function arity
210 ,Instr "ldl" [Lit t] "get pointer again"
211 ,Instr "ldh" [Lit 1] "get function arity"
212 ,Instr "ldc" [Lit $ length es] "add argument number"
213 ,Instr "add" [] "add"
214 ,Instr "sth" [] "Store"
215 ,Instr "ajs" [Lit -1] "Adjust pointer"
216 //load the arguments
217 ,Instr "ldl" [Lit t] ""
218 ,Instr "ldh" [Lit 1] "Load available arguments"
219 ,Instr "str" [Raw "R5"] "Store available args in register"
220 ,Instr "ldc" [Lit 0] "Store offset"
221 ,Instr "str" [Raw "R6"] "Store offset in register"
222 ,Lab start
223 ,Instr "ldr" [Raw "R5"] ""
224 ,Instr "ldc" [Lit 0] ""
225 ,Instr "eq" [] ""
226 ,Instr "brt" [L finish] "Done pushing arg, bye"
227 //Load heapadress
228 ,Instr "ldl" [Lit t] ""
229 ,Instr "ldr" [Raw "R6"] ""
230 ,Instr "add" [] "Corrected heapaddress"
231 ,Instr "ldh" [Lit 2] "Load argument"
232 ,Instr "sth" [] "And store it immediatly after"
233 //Decrease available arguments
234 ,Instr "ldr" [Raw "R5"] ""
235 ,Instr "ldc" [Lit 1] ""
236 ,Instr "sub" [] ""
237 ,Instr "str" [Raw "R5"] ""
238 //Increase available arguments
239 ,Instr "ldr" [Raw "R6"] ""
240 ,Instr "ldc" [Lit 1] ""
241 ,Instr "add" [] ""
242 ,Instr "str" [Raw "R6"] ""
243 ,Instr "bra" [L start] ""
244 ,Lab finish
245 ]
246 >>| mapM_ g es
247 >>| tell
248 [Instr "stmh" [Lit $ length es] "Store extra args"
249 ,Instr "ajs" [Lit -1] ""]
250 )
251 //Function is complete
252 ( fresh >>= \finish->fresh >>= \start->tell [
253 Instr "ldl" [Lit t] "STARTING HIGHER ORDER CALL"
254 ,Instr "ldh" [Lit 1] "Load available arguments"
255 ,Instr "str" [Raw "R5"] "Store available args in register"
256 ,Instr "ldc" [Lit 0] "Store offset"
257 ,Instr "str" [Raw "R6"] "Store offset in register"
258
259 ,Lab start
260 ,Instr "ldr" [Raw "R5"] ""
261 ,Instr "ldc" [Lit 0] ""
262 ,Instr "eq" [] ""
263 ,Instr "brt" [L finish] "Done pushing arg, bye"
264 //Load heapadress
265 ,Instr "ldl" [Lit t] ""
266 ,Instr "ldr" [Raw "R6"] ""
267 ,Instr "add" [] "Corrected heapaddress"
268 ,Instr "ldh" [Lit 2] "Load argument"
269 //Decrease available arguments
270 ,Instr "ldr" [Raw "R5"] ""
271 ,Instr "ldc" [Lit 1] ""
272 ,Instr "sub" [] ""
273 ,Instr "str" [Raw "R5"] ""
274 //Increase available arguments
275 ,Instr "ldr" [Raw "R6"] ""
276 ,Instr "ldc" [Lit 1] ""
277 ,Instr "add" [] ""
278 ,Instr "str" [Raw "R6"] ""
279 ,Instr "bra" [L start] ""
280 ,Lab finish
281 ]
282 >>| mapM_ g es
283 >>| tell
284 [Instr "ldl" [Lit t] ""
285 ,Instr "ldh" [Lit 0] "Get function number"
286 ,Instr "str" [Raw "R5"] ""
287 ,Instr "bsr" [L "1func"] "HIGHER ORDER END"
288 ,Instr "ldl" [Lit t] ""
289 ,Instr "ldh" [Lit 1] ""
290 ,Instr "neg" [] ""
291 ,Instr "ldr" [Raw "SP"] ""
292 ,Instr "add" [] ""
293 ,Instr "ldc" [Lit $ length es + 1] ""
294 ,Instr "sub" [] ""
295 ,Instr "str" [Raw "SP"] ""
296 ,Instr "ldr" [Raw "RR"] ""
297 ]
298 )
299 Nothing = liftT (Left $ Error "Undefined function!!!")
300
301 jump :: String String -> Gen ()
302 jump instr k = getAdressbook >>= \ab->case 'Map'.get k ab of
303 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
304 Just (LAB t _ _) = tell [Instr instr [L t] (k +++"()")]
305 Just (ADDR t arity) = abort "NO ADDRESS JUMPING FFS"
306
307 instance g Stmt where
308 g (IfStmt cond th el) =
309 fresh >>= \elseLabel->
310 fresh >>= \endLabel->
311 g cond >>|
312 tell [Instr "brf" [L elseLabel] "branch else"] >>|
313 mapM_ g th >>|
314 tell [Instr "bra" [L endLabel] "branch end if"] >>|
315 tell [Lab elseLabel] >>|
316 mapM_ g el >>|
317 tell [Lab endLabel]
318 g (WhileStmt cond th) =
319 fresh >>= \startLabel->
320 fresh >>= \endLabel ->
321 tell [Lab startLabel] >>|
322 g cond >>|
323 tell [Instr "brf" [L endLabel] "branch end while"] >>|
324 mapM_ g th >>|
325 tell [Instr "bra" [L startLabel] "branch start while"] >>|
326 tell [Lab endLabel]
327 g (AssStmt (VarDef k fs) e) =
328 g e >>| getAdressbook >>= \ab->case 'Map'.get k ab of
329 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as var"])
330 Just (LAB t _ _) = liftT (Left $ Error $ "PANIC: cannot assign to function")
331 Just (ADDR t ar) = tell [Instr "stl" [Lit t] ""]
332 g (FunStmt k es fs) = mapM_ g es
333 >>| jump "bsr" k
334 >>| tell [Instr "ajs" [Lit (~(length es))] ""] //clean up args
335 >>| mapM_ g fs
336 >>| pure ()
337 g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""]
338 >>| tell [Instr "ret" [] ""]
339 g (ReturnStmt (Just e)) = g e
340 >>| tell [Instr "str" [Raw "RR"] ""]
341 >>| g (ReturnStmt Nothing)
342
343 foldVarDecl :: Int VarDecl -> Gen Int
344 foldVarDecl x (VarDecl _ mt k e) = g e
345 >>| annote x k
346 >>| updateAdressbook (extend k (ADDR x $ arity $ fromJust mt))
347 >>| pure (x + 1)
348
349 arity :: Type -> Int
350 arity (_ ->> x) = 1 + arity x
351 arity _ = 0
352
353 addVars :: Type [String] -> (Addressbook -> Addressbook)
354 addVars _ [] = id
355 addVars (t ->> ts) [x:xs] = \ab->
356 extend x (ADDR (-2 - (length xs)) (arity t)) (addVars ts xs ab)
357 addVars t [x] = \ab->
358 extend x (ADDR -2 0) ab
359
360 instance g FunDecl where
361 g (FunDecl _ k args mt vds stms) =
362 //varDecls can call the enclosing function, so first reserve a label for it
363 getAdressbook >>= \oldMap ->
364 updateAdressbook (addVars (fromJust mt) args) >>|
365 tell [Lab k] >>|
366 tell [Instr "link" [Lit 0] ""] >>|
367 //add the vars
368 foldM foldVarDecl 1 vds >>|
369 //and the statements
370 mapM_ g stms >>|
371 //Ugly hack to always return
372 g (ReturnStmt Nothing) >>|
373 updateAdressbook (const oldMap) >>| pure ()
374
375 annote :: Int String -> Gen ()
376 annote pos key =
377 tell [Instr "annote" [Raw "MP", Lit pos, Lit pos, Raw "black", Raw key] ""]
378
379 class print a :: a -> [String]
380
381 instance print Instr where
382 print (Lab l) = [l, ":", "\n"]
383 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
384
385 instance print [Arg] where
386 print args = (map toString args)
387
388 instance toString Arg where
389 toString (L l) = l
390 toString (Lit int) = toString int
391 toString (Raw s) = s
392
393 instance toString SSMProgram where
394 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p