1 implementation module gen
7 from StdFunc import id, const
12 import qualified Data.Map as Map
19 import Control.Applicative
21 import Control.Monad.Trans
22 from Text import class Text(concat), instance Text String
29 :: Instr = Instr String [Arg] 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
39 labelStream :: [Label]
40 labelStream = ["lbl_" +++ toString i\\i<-[1..]]
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)
50 addFuncs [] _ = 'Map'.newMap
51 addFuncs [(FunDecl _ k args _ _ _):xs] n =
52 extend k (LAB k (length args) n) $ addFuncs xs (n+1)
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
60 Instr "bsr" [L "main"] "",
62 ] >>| tell (programContext fds)
65 programContext :: [FunDecl] -> SSMProgram
66 programContext x = [Lab "1func"
67 :fS ["1printint" ,"1printchar"
69 ,"isEmpty":map (\(FunDecl _ k _ _ _ _)->k) x] 0] ++ context
72 fS :: [String] Int -> SSMProgram
75 Lab $ "1next" +++ toString n
76 ,Instr "ldr" [Raw "R5"] ""
77 ,Instr "ldc" [Lit n] $ "branch to: " +++ k
79 ,Instr "brf" [L $ "1next" +++ (toString $ n + 1)] ""
83 context = [Lab "1printint"
84 ,Instr "link" [Lit 0] ""
85 ,Instr "ldl" [Lit -2] "load first argument"
86 ,Instr "trap" [Lit 0] "print int"
90 ,Instr "link" [Lit 0] ""
91 ,Instr "ldl" [Lit -2] "load first argument"
92 ,Instr "trap" [Lit 1] "print char"
96 ,Instr "link" [Lit 0] ""
97 ,Instr "trap" [Lit 11] "read char"
98 ,Instr "str" [Raw "RR"] ""
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" [] ""
113 getAdressbook :: Gen Addressbook
114 getAdressbook = gets fst
116 updateAdressbook :: (Addressbook -> Addressbook) -> Gen Addressbook
117 updateAdressbook f = modify (appFst f) >>| getAdressbook
119 extend :: String Address Addressbook -> Addressbook
120 extend k pl g = 'Map'.put k pl g
123 fresh = gets snd >>= \vars->
124 modify (appSnd $ const $ tail vars) >>|
127 class g a :: a -> Gen ()
130 g UnNegation = tell [Instr "not" [] ""]
131 g UnMinus = tell [Instr "neg" [] ""]
134 g o = tell [Instr s [] ""]
150 BiCons = abort "Shit, Cons, how to deal with this?"
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"]
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
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] ""
178 ,Instr "ldc" [Lit 0] ""
180 ,Instr "ajs" [Lit -1] ""]
181 Nothing = liftT $ Left $ Error "PANIC: unresolver variable expr"
182 g (LambdaExpr _ _ _) = liftT $ Left $ Error "PANIC: Lambdas should be unfolded"
183 g (FunExpr _ k es fs) = funnyStuff k es fs
185 funnyStuff :: String [Expr] [FieldSelector] -> Gen ()
186 funnyStuff k es fs = getAdressbook >>= \ab->case 'Map'.get k ab of
187 //Identifier points to function
188 Just (LAB l arity fn) = if (arity <> (length es))
189 //Function is not complete
191 [Instr "ldc" [Lit fn] "Store function number"
193 ,Instr "ldc" [Lit $ length es] "Store arity"
195 ,Instr "ajs" [Lit -1] ""]
197 >>| if (isEmpty es) (pure ()) (tell
198 [Instr "stmh" [Lit $ length es] "Store arguments"
199 ,Instr "ajs" [Lit -1] ""]))
200 //Function is complete
202 >>| getAdressbook >>= \ab->(case 'Map'.get k ab of
203 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
204 Just (LAB t _ _) = tell [Instr "bsr" [L t] (k +++"()")]
205 Just (ADDR t arity) = liftT (Left $ Error "NO ADDRESS JUMPING FFS")
208 [Instr "ajs" [Lit $ ~(length es)] "Clean arguments"
209 ,Instr "ldr" [Raw "RR"] ""])
210 //Identifier points to variable, thus higher order function
211 Just (ADDR t arity) = if (arity <> (length es))
212 //Function is still not complete
213 ( fresh >>= \finish->fresh >>= \start->tell [
214 //Store function number
215 Instr "ldl" [Lit t] "STARTING HIGHER ORDER UPDATE"
216 ,Instr "ldh" [Lit 0] "get function number"
217 ,Instr "sth" [] "Store"
218 //Store function arity
219 ,Instr "ldl" [Lit t] "get pointer again"
220 ,Instr "ldh" [Lit 1] "get function arity"
221 ,Instr "ldc" [Lit $ length es] "add argument number"
222 ,Instr "add" [] "add"
223 ,Instr "sth" [] "Store"
224 ,Instr "ajs" [Lit -1] "Adjust pointer"
226 ,Instr "ldl" [Lit t] ""
227 ,Instr "ldh" [Lit 1] "Load available arguments"
228 ,Instr "str" [Raw "R5"] "Store available args in register"
229 ,Instr "ldc" [Lit 0] "Store offset"
230 ,Instr "str" [Raw "R6"] "Store offset in register"
232 ,Instr "ldr" [Raw "R5"] ""
233 ,Instr "ldc" [Lit 0] ""
235 ,Instr "brt" [L finish] "Done pushing arg, bye"
237 ,Instr "ldl" [Lit t] ""
238 ,Instr "ldr" [Raw "R6"] ""
239 ,Instr "add" [] "Corrected heapaddress"
240 ,Instr "ldh" [Lit 2] "Load argument"
241 ,Instr "sth" [] "And store it immediatly after"
242 //Decrease available arguments
243 ,Instr "ldr" [Raw "R5"] ""
244 ,Instr "ldc" [Lit 1] ""
246 ,Instr "str" [Raw "R5"] ""
247 //Increase available arguments
248 ,Instr "ldr" [Raw "R6"] ""
249 ,Instr "ldc" [Lit 1] ""
251 ,Instr "str" [Raw "R6"] ""
252 ,Instr "bra" [L start] ""
257 [Instr "stmh" [Lit $ length es] "Store extra args"
258 ,Instr "ajs" [Lit -1] ""]
260 //Function is complete
261 ( fresh >>= \finish->fresh >>= \start->tell [
262 Instr "ldl" [Lit t] "STARTING HIGHER ORDER CALL"
263 ,Instr "ldh" [Lit 1] "Load available arguments"
264 ,Instr "str" [Raw "R5"] "Store available args in register"
265 ,Instr "ldc" [Lit 0] "Store offset"
266 ,Instr "str" [Raw "R6"] "Store offset in register"
269 ,Instr "ldr" [Raw "R5"] ""
270 ,Instr "ldc" [Lit 0] ""
272 ,Instr "brt" [L finish] "Done pushing arg, bye"
274 ,Instr "ldl" [Lit t] ""
275 ,Instr "ldr" [Raw "R6"] ""
276 ,Instr "add" [] "Corrected heapaddress"
277 ,Instr "ldh" [Lit 2] "Load argument"
278 //Decrease available arguments
279 ,Instr "ldr" [Raw "R5"] ""
280 ,Instr "ldc" [Lit 1] ""
282 ,Instr "str" [Raw "R5"] ""
283 //Increase available arguments
284 ,Instr "ldr" [Raw "R6"] ""
285 ,Instr "ldc" [Lit 1] ""
287 ,Instr "str" [Raw "R6"] ""
288 ,Instr "bra" [L start] ""
293 [Instr "ldl" [Lit t] ""
294 ,Instr "ldh" [Lit 0] "Get function number"
295 ,Instr "str" [Raw "R5"] ""
296 ,Instr "bsr" [L "1func"] "HIGHER ORDER END"
297 ,Instr "ldl" [Lit t] ""
298 ,Instr "ldh" [Lit 1] ""
300 ,Instr "ldr" [Raw "SP"] ""
302 ,Instr "ldc" [Lit $ length es + 1] ""
304 ,Instr "str" [Raw "SP"] ""
305 ,Instr "ldr" [Raw "RR"] ""
308 Nothing = liftT (Left $ Error $ "PANIC: Undefined function: " +++ k)
310 instance g Stmt where
311 g (IfStmt cond th el) =
312 fresh >>= \elseLabel->
313 fresh >>= \endLabel->
315 tell [Instr "brf" [L elseLabel] "branch else"] >>|
317 tell [Instr "bra" [L endLabel] "branch end if"] >>|
318 tell [Lab elseLabel] >>|
321 g (WhileStmt cond th) =
322 fresh >>= \startLabel->
323 fresh >>= \endLabel ->
324 tell [Lab startLabel] >>|
326 tell [Instr "brf" [L endLabel] "branch end while"] >>|
328 tell [Instr "bra" [L startLabel] "branch start while"] >>|
330 g (AssStmt (VarDef k fs) e) =
331 g e >>| getAdressbook >>= \ab->case 'Map'.get k ab of
332 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as var"])
333 Just (LAB t _ _) = liftT (Left $ Error $ "PANIC: cannot assign to function")
334 Just (ADDR t ar) = tell [Instr "stl" [Lit t] ""]
335 g (FunStmt k es fs) = funnyStuff k es fs
336 g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""]
337 >>| tell [Instr "ret" [] ""]
338 g (ReturnStmt (Just e)) = g e
339 >>| tell [Instr "str" [Raw "RR"] ""]
340 >>| g (ReturnStmt Nothing)
342 foldVarDecl :: Int VarDecl -> Gen Int
343 foldVarDecl x (VarDecl _ mt k e) = g e
345 >>| updateAdressbook (extend k (ADDR x $ arity $ fromJust mt))
349 arity (_ ->> x) = 1 + arity x
352 addVars :: Type [String] -> (Addressbook -> Addressbook)
354 addVars (t ->> ts) [x:xs] = \ab->
355 extend x (ADDR (-2 - (length xs)) (arity t)) (addVars ts xs ab)
356 addVars t [x] = \ab->
357 extend x (ADDR -2 0) ab
359 instance g FunDecl where
360 g (FunDecl _ k args mt vds stms) =
361 //varDecls can call the enclosing function, so first reserve a label for it
362 getAdressbook >>= \oldMap ->
363 updateAdressbook (addVars (fromJust mt) args) >>|
365 tell [Instr "link" [Lit 0] ""] >>|
367 foldM foldVarDecl 1 vds >>|
370 //Ugly hack to always return
371 g (ReturnStmt Nothing) >>|
372 updateAdressbook (const oldMap) >>| pure ()
374 annote :: Int String -> Gen ()
376 tell [Instr "annote" [Raw "MP", Lit pos, Lit pos, Raw "black", Raw key] ""]
378 class print a :: a -> [String]
380 instance print Instr where
381 print (Lab l) = [l, ":", "\n"]
382 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
384 instance print [Arg] where
385 print args = (map toString args)
387 instance toString Arg where
389 toString (Lit int) = toString int
392 instance toString SSMProgram where
393 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p