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 | ADDR Int
37 :: Gen a :== RWST () SSMProgram (Addressbook, [Label]) (Either GenError) a
39 labelStream :: [Label]
40 labelStream = ["lbl_" +++ toString i\\i<-[1..]]
42 defaultAddressBook :: Addressbook
43 defaultAddressBook = extend "print" (LAB "print")
44 $ extend "read" (LAB "read")
45 $ extend "isEmpty" (LAB "isEmpty")
48 gen :: AST -> Either String String
49 gen (AST fds) = case evalRWST prog () (defaultAddressBook, labelStream) of
50 Left (Error e) = Left e
51 Right (_, p) = Right $ toString p
54 Instr "bsr" [L "main"] "",
56 ] >>| tell programContext
59 programContext :: SSMProgram
60 programContext = [Lab "print" //there is no actual IO in SSM
61 ,Instr "link" [Lit 0] ""
64 ,Lab "read" //there is no actual IO in SSM
65 ,Instr "link" [Lit 0] ""
66 ,Instr "ldc" [Lit 0] ""
68 ,Instr "str" [Raw "RR"] ""
72 ,Instr "link" [Lit 0] ""
73 ,Instr "ldl" [Lit -2] "load prt to list"
74 ,Instr "lda" [Lit 0] "derefrence ptr"
75 ,Instr "ldc" [Lit 0] ""
76 ,Instr "eq" [] "test for null pointer"
77 ,Instr "str" [Raw "RR"] ""
82 //helper functions for the gen monad
83 getAdressbook :: Gen Addressbook
84 getAdressbook = gets fst
86 updateAdressbook :: (Addressbook -> Addressbook) -> Gen Addressbook
87 updateAdressbook f = modify (appFst f) >>| getAdressbook
89 extend :: String Address Addressbook -> Addressbook
90 extend k pl g = 'Map'.put k pl g
93 fresh = gets snd >>= \vars->
94 modify (appSnd $ const $ tail vars) >>|
97 class g a :: a -> Gen ()
100 g UnNegation = tell [Instr "not" [] ""]
101 g UnMinus = tell [Instr "neg" [] ""]
104 g o = tell [Instr s [] ""]
120 BiCons = abort "Shit, Cons, how to deal with this?"
122 instance g FieldSelector where
123 g FieldFst = tell [Instr "lda" [Lit 0] "fst"]
124 g FieldSnd = tell [Instr "lda" [Lit 1] "snd"]
125 g FieldHd = tell [Instr "lda" [Lit -1] "hd"]
126 g FieldTl = tell [Instr "lda" [Lit 0] "tl"]
128 instance g Expr where
129 g (IntExpr _ i) = tell [Instr "ldc" [Lit i] ""]
130 g (CharExpr _ c) = tell [Instr "ldc" [Lit (toInt c)] ""]
131 g (BoolExpr _ b) = tell [Instr "ldc" [Lit (if b TRUE FALSE)] ""]
132 g (EmptyListExpr _) = tell [Instr "ldc" [Lit 0] ""]
133 >>| tell [Instr "sth" [] ""]
134 g (Op1Expr _ o e) = g e >>| g o
135 g (Op2Expr _ e1 BiCons e2) = g e2 >>| g e1
136 >>| tell [Instr "sth" [] ""]
137 >>| tell [Instr "ajs" [Lit -1] ""]
138 >>| tell [Instr "sth" [] ""]
139 g (Op2Expr _ e1 op e2) = g e1 >>| g e2 >>| g op
140 g (TupleExpr _ (e1,e2)) = g e1
141 >>| tell [Instr "sth" [] ""]
143 >>| tell [Instr "sth" [] ""]
144 >>| tell [Instr "ajs" [Lit -1] ""]
145 g (VarExpr _ (VarDef k fs)) = getAdressbook >>= \ab->case 'Map'.get k ab of
146 Just (ADDR t) = tell [Instr "ldl" [Lit t] ""] >>| mapM_ g fs >>| pure ()
147 _ = liftT (Left $ Error "PANIC: variable and function name clash")
148 g (FunExpr _ k es fs) =
151 >>| mapM_ (const $ tell [Instr "ajs" [Lit -1] ""]) es //clean up args
152 >>| tell [Instr "ldr" [Raw "RR"] ""]
154 jump :: String String -> Gen ()
155 jump instr k = getAdressbook >>= \ab->case 'Map'.get k ab of
156 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
157 Just (LAB t) = tell [Instr instr [L t] (k +++"()")]
158 Just (ADDR t) = liftT (Left $ Error $ "PANIC: jump should go to label")
160 instance g Stmt where
161 g (IfStmt cond th el) =
162 fresh >>= \elseLabel->
163 fresh >>= \endLabel->
165 tell [Instr "brf" [L elseLabel] "branch else"] >>|
167 tell [Instr "bra" [L endLabel] "branch end if"] >>|
168 tell [Lab elseLabel] >>|
171 g (WhileStmt cond th) =
172 fresh >>= \startLabel->
173 fresh >>= \endLabel ->
174 tell [Lab startLabel] >>|
176 tell [Instr "brf" [L endLabel] "branch end while"] >>|
178 tell [Instr "bra" [L startLabel] "branch start while"] >>|
180 g (AssStmt (VarDef k fs) e) =
181 g e >>| getAdressbook >>= \ab->case 'Map'.get k ab of
182 Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as var"])
183 Just (LAB t) = liftT (Left $ Error $ "PANIC: cannot assign to function")
184 Just (ADDR t) = tell [Instr "stl" [Lit t] ""]
185 g (FunStmt k es) = mapM_ g es
187 >>| mapM_ (const $ tell [Instr "ajs" [Lit -1] ""]) es //clean up args
189 g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""]
190 >>| tell [Instr "ret" [] ""]
191 g (ReturnStmt (Just e)) = g e
192 >>| tell [Instr "str" [Raw "RR"] ""]
193 >>| g (ReturnStmt Nothing)
195 foldVarDecl :: Int VarDecl -> Gen Int
196 foldVarDecl x (VarDecl _ _ k e) = g e
198 >>| updateAdressbook (extend k (ADDR x)) >>| pure (x + 1)
200 addVars :: [String] -> (Addressbook -> Addressbook)
202 addVars [x:xs] = \ab->extend x (ADDR (-2 - (length xs))) (addVars xs ab)
204 instance g FunDecl where
205 g (FunDecl _ k args _ vds stms) =
206 //varDecls can call the enclosing function, so first reserve a label for it
207 updateAdressbook (extend k (LAB k)) >>|
208 getAdressbook >>= \oldMap ->
209 updateAdressbook (addVars args) >>|
211 tell [Instr "link" [Lit 0] ""] >>|
213 foldM foldVarDecl 1 vds >>|
216 updateAdressbook (const oldMap) >>| pure ()
218 annote :: Int String -> Gen ()
220 tell [Instr "annote" [Raw "MP", Lit pos, Lit pos, Raw "green", Raw key] ""]
222 class print a :: a -> [String]
224 instance print Instr where
225 print (Lab l) = [l, ":", "\n"]
226 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
228 instance print [Arg] where
229 print args = (map toString args)
231 instance toString Arg where
233 toString (Lit int) = toString int
236 instance toString SSMProgram where
237 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p