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 :: GenMap :== 'Map'.Map String LoadPlace
36 //completely change to either Stack, Heap, Register?
37 :: LoadPlace = LDA Int | LDC Int | LDH Int | LDL Int
40 :: Gen a :== RWST Label SSMProgram (GenMap, [Label]) (Either GenError) a
42 labelStream :: [Label]
43 labelStream = ["lbl_" +++ toString i\\i<-[1..]]
45 gen :: AST -> Either String String
46 gen (AST fds) = case evalRWST prog "" ('Map'.newMap, labelStream) of
47 Left (Error e) = Left e
48 Right (_, p) = Right $ toString p
50 prog = tell [Instr "bra" [L "main"] ""] >>| mapM_ g fds
53 //All VarDecls are added as function, how to deal with assignments?
54 // (And when we deal with assignments, how to deal with assignments to higher order functions?)
55 //Dealing with arguments
56 //Dealing with types that do not fit on the Stack
57 // Probably completely change LoadPlace to a Type and a position relative to *something*
58 // And where the type determines if this position is a pointer to the heap or an
61 //helper functions for the gen monad
65 changeGenMap :: (GenMap -> GenMap) -> Gen GenMap
66 changeGenMap f = modify (appFst f) >>| genMap
68 extend :: String LoadPlace GenMap -> GenMap
69 extend k pl g = 'Map'.put k pl g
72 fresh = gets snd >>= \vars->
73 modify (appSnd $ const $ tail vars) >>|
76 class g a :: a -> Gen ()
79 g (VarExpr _ (VarDef k fs)) = load k >>= \instr-> tell [instr] //note: pure is pure for list, i.e. []
80 g (IntExpr _ i) = loadP (LDC i) >>= \instr-> tell [instr]
81 g (CharExpr _ c) = abort "How to deal with chars?"
82 g (BoolExpr _ True) = loadP (LDC TRUE) >>= \instr-> tell [instr]
83 g (BoolExpr _ False) = loadP (LDC FALSE) >>= \instr-> tell [instr]
84 g (Op2Expr _ e1 op e2) = g e1 >>| g e2 >>| tell [Instr (op2ins op) [] ""]
85 g (Op1Expr _ UnNegation e) = g e >>| tell [Instr "not" [] ""]
86 g (Op1Expr _ UnMinus e) = g e >>| tell [Instr "neg" [] ""]
87 g (EmptyListExpr _) = abort "Shit, empty list expr"
88 g (TupleExpr p (e1,e2)) = abort "How to deal with tuples?"
89 g (FunExpr _ k es fs) =
90 mapM g es >>| //put all arguments on the stack (todo: fix argument handling!)
91 jump "bsr" k >>= \instr->
92 tell [instr] >>| //actually branch to function
93 tell [Instr "ldr" [Raw "RR"] ""] //push return value on stack, todo: check for VOID
96 g (IfStmt cond th el) =
97 fresh >>= \elseLabel->
100 tell [Instr "brf" [L elseLabel] "branch else"] >>|
102 tell [Instr "bra" [L endLabel] "branch end if"] >>|
103 tell [Lab elseLabel] >>|
106 g (WhileStmt cond th) =
107 fresh >>= \startLabel->
108 fresh >>= \endLabel ->
109 tell [Lab startLabel] >>|
111 tell [Instr "brf" [L endLabel] "branch end while"] >>|
113 tell [Instr "bra" [L startLabel] "branch start while"] >>|
115 g (AssStmt (VarDef k fs) e) =
117 abort "Shit, an assignment, figure out something with storing vars or something"
118 //vars will be on stack in locals (possible pointers to heap)
119 g (FunStmt _ _) = abort "CodeGen, FunStmt unused" //not used
120 g (ReturnStmt Nothing) = tell [Instr "ret" [] ""] //NOTE! Assumes only return address on stack, safe?
121 g (ReturnStmt (Just e)) =
123 tell [Instr "str" [Raw "RR"] ""] >>|
124 g (ReturnStmt Nothing)
126 instance g VarDecl where
127 g (VarDecl _ Nothing _ _) = liftT (Left $ Error "PANIC: untyped vardecl")
128 g (VarDecl _ (Just t) k e) =
129 (\l->k+++"_"+++l) <$> fresh >>= \lbl->
130 changeGenMap (extend k (FUNC lbl)) >>|
133 tell [Instr "str" [Raw "RR"] ""] >>|
134 tell [Instr "ret" [] ""]
137 instance g FunDecl where
138 g (FunDecl _ k _ _ vds stms) =
139 //varDecls can call the enclosing function, so first reserve a label for it
140 (\l-> if (k=="main") "main" (l+++"_"+++k)) <$> fresh >>= \lbl->
141 changeGenMap (extend k (FUNC lbl)) >>|
142 //then generate functions for the VarDecls
143 genMap >>= \oldMap ->
145 //then the main function
148 changeGenMap (const oldMap) >>| pure ()
150 op2ins :: Op2 -> String
151 op2ins op = case op of
165 BiCons = abort "Shit, Cons, how to deal with this?"
167 load :: String -> Gen Instr
168 load k = genMap >>= \g-> case 'Map'.member k g of
169 False = liftT (Left $ Error $ concat ["PANIC: ", k, " not found in variable mapping"])
170 True = loadP $ 'Map'.find k g
172 loadP :: LoadPlace -> Gen Instr
173 loadP pl = dec pl >>= \(instr, arg)-> pure $ Instr instr [arg] ""
175 dec (LDA i) = pure ("lda", Lit i)
176 dec (LDC i) = pure ("ldc", Lit i)
177 dec (LDH i) = pure ("ldh", Lit i)
178 dec (LDL i) = pure ("ldl", Lit i)
179 dec (LDR i) = pure ("ldr", Lit i)
180 dec (LDS i) = pure ("lds", Lit i)
181 dec _ = liftT (Left $ Error "PANIC: trying to load non adres")
183 //Instruction (String), key of function to jump to
184 jump :: String String -> Gen Instr
185 jump instr k = genMap >>= \g-> case 'Map'.member k g of
186 False = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
187 True = dec ('Map'.find k g) >>= \lbl-> pure $ Instr instr [lbl] (k +++"()")
189 dec (FUNC l) = pure (L l)
190 dec _ = liftT (Left $ Error "PANIC: trying to jump to non label")
192 class print a :: a -> [String]
194 instance print Instr where
195 print (Lab l) = [l, ":", "\n"]
196 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
198 instance print [Arg] where
199 print args = (map toString args)
201 instance toString Arg where
203 toString (Lit int) = toString int
206 instance toString SSMProgram where
207 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p