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
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 Instr "lds" [Lit 0] ""
76 ,Instr "ldc" [Lit n] $ "branch to: " +++ k
78 ,Instr "ajs" [Lit -1] ""
82 context = [Lab "1printint"
83 ,Instr "link" [Lit 0] ""
84 ,Instr "ldl" [Lit -2] "load first argument"
85 ,Instr "trap" [Lit 0] "print int"
89 ,Instr "link" [Lit 0] ""
90 ,Instr "ldl" [Lit -2] "load first argument"
91 ,Instr "trap" [Lit 1] "print char"
95 ,Instr "link" [Lit 0] ""
96 ,Instr "trap" [Lit 11] "read char"
97 ,Instr "str" [Raw "RR"] ""
101 ,Instr "link" [Lit 0] ""
102 ,Instr "ldl" [Lit -2] "load prt to list"
103 ,Instr "lda" [Lit 0] "derefrence ptr"
104 ,Instr "ldc" [Lit 0] ""
105 ,Instr "eq" [] "test for null pointer"
106 ,Instr "str" [Raw "RR"] ""
107 ,Instr "unlink" [] ""
112 getAdressbook :: Gen Addressbook
113 getAdressbook = gets fst
115 updateAdressbook :: (Addressbook -> Addressbook) -> Gen Addressbook
116 updateAdressbook f = modify (appFst f) >>| getAdressbook
118 extend :: String Address Addressbook -> Addressbook
119 extend k pl g = 'Map'.put k pl g
122 fresh = gets snd >>= \vars->
123 modify (appSnd $ const $ tail vars) >>|
126 class g a :: a -> Gen ()
129 g UnNegation = tell [Instr "not" [] ""]
130 g UnMinus = tell [Instr "neg" [] ""]
133 g o = tell [Instr s [] ""]
149 BiCons = abort "Shit, Cons, how to deal with this?"
151 instance g FieldSelector where
152 g FieldFst = tell [Instr "lda" [Lit -1] "fst"]
153 g FieldSnd = tell [Instr "lda" [Lit 0] "snd"]
154 g FieldHd = tell [Instr "lda" [Lit -1] "hd"]
155 g FieldTl = tell [Instr "lda" [Lit 0] "tl"]
157 instance g Expr where
158 g (IntExpr _ i) = tell [Instr "ldc" [Lit i] ""]
159 g (CharExpr _ c) = tell [Instr "ldc" [Lit (toInt c)] ""]
160 g (BoolExpr _ b) = tell [Instr "ldc" [Lit (if b TRUE FALSE)] ""]
161 g (EmptyListExpr _) = tell [Instr "ldc" [Lit 0] ""]
162 >>| tell [Instr "sth" [] ""]
163 g (Op1Expr _ o e) = g e >>| g o
164 g (Op2Expr _ e1 BiCons e2) = g e2 >>| g e1
165 >>| tell [Instr "sth" [] ""]
166 >>| tell [Instr "ajs" [Lit -1] ""]
167 >>| tell [Instr "sth" [] ""]
168 g (Op2Expr _ e1 op e2) = g e1 >>| g e2 >>| g op
169 g (TupleExpr _ (e1,e2)) = g e1
171 >>| tell [Instr "stmh" [Lit 2] ""]
172 g (VarExpr _ (VarDef k fs)) = getAdressbook >>= \ab->case 'Map'.get k ab of
173 Just (ADDR t) = tell [Instr "ldl" [Lit t] ""] >>| mapM_ g fs >>| pure ()
174 Just (LAB l _ fn) = tell
175 [Instr "ldc" [Lit fn] ""
177 ,Instr "ldc" [Lit 0] ""
179 g (FunExpr _ k es fs) = getAdressbook >>= \ab->case 'Map'.get k ab of
180 //Identifier points to function
181 Just (LAB l arity fn) = if (arity <> (length es))
183 [Instr "ldc" [Lit fn] "Store function number"
185 ,Instr "ldc" [Lit $ length es] "Store arity"
187 ,Instr "ajs" [Lit -1] ""]
189 >>| if (isEmpty es) (pure ()) (tell
190 [Instr "stmh" [Lit $ length es] "Store arguments"
191 ,Instr "ajs" [Lit -1] ""]))
195 [Instr "ajs" [Lit $ ~(length es)] "Clean arguments"
196 ,Instr "ldr" [Raw "RR"] ""])
197 //Identifier points to variable, thus higher order function
198 Just (ADDR t) = liftT (Left $ Error "FunExpr to addr")
199 Nothing = liftT (Left $ Error "Undefined function!!!")
201 jump :: String String -> Gen ()
202 jump instr k = 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 instr [L t] (k +++"()")]
205 Just (ADDR t) = liftT (Left $ Error "Address as jump???")
207 instance g Stmt where
208 g (IfStmt cond th el) =
209 fresh >>= \elseLabel->
210 fresh >>= \endLabel->
212 tell [Instr "brf" [L elseLabel] "branch else"] >>|
214 tell [Instr "bra" [L endLabel] "branch end if"] >>|
215 tell [Lab elseLabel] >>|
218 g (WhileStmt cond th) =
219 fresh >>= \startLabel->
220 fresh >>= \endLabel ->
221 tell [Lab startLabel] >>|
223 tell [Instr "brf" [L endLabel] "branch end while"] >>|
225 tell [Instr "bra" [L startLabel] "branch start while"] >>|
227 g (AssStmt (VarDef k fs) e) =
228 g e >>| getAdressbook >>= \ab->case 'Map'.get k ab of
229 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as var"])
230 Just (LAB t _ _) = liftT (Left $ Error $ "PANIC: cannot assign to function")
231 Just (ADDR t) = tell [Instr "stl" [Lit t] ""]
232 g (FunStmt k es fs) = mapM_ g es
234 >>| tell [Instr "ajs" [Lit (~(length es))] ""] //clean up args
237 g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""]
238 >>| tell [Instr "ret" [] ""]
239 g (ReturnStmt (Just e)) = g e
240 >>| tell [Instr "str" [Raw "RR"] ""]
241 >>| g (ReturnStmt Nothing)
243 foldVarDecl :: Int VarDecl -> Gen Int
244 foldVarDecl x (VarDecl _ mt k e) = g e
246 >>| updateAdressbook (extend k (ADDR x))
249 addVars :: [String] -> (Addressbook -> Addressbook)
251 addVars [x:xs] = \ab->extend x (ADDR (-2 - (length xs))) (addVars xs ab)
253 instance g FunDecl where
254 g (FunDecl _ k args _ vds stms) =
255 //varDecls can call the enclosing function, so first reserve a label for it
256 getAdressbook >>= \oldMap ->
257 updateAdressbook (addVars args) >>|
259 tell [Instr "link" [Lit 0] ""] >>|
261 foldM foldVarDecl 1 vds >>|
264 //Ugly hack to always return
265 g (ReturnStmt Nothing) >>|
266 updateAdressbook (const oldMap) >>| pure ()
268 annote :: Int String -> Gen ()
270 tell [Instr "annote" [Raw "MP", Lit pos, Lit pos, Raw "green", Raw key] ""]
272 class print a :: a -> [String]
274 instance print Instr where
275 print (Lab l) = [l, ":", "\n"]
276 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
278 instance print [Arg] where
279 print args = (map toString args)
281 instance toString Arg where
283 toString (Lit int) = toString int
286 instance toString SSMProgram where
287 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p