1 implementation module gen
11 from StdEnv import const
14 import qualified Data.Map as Map
21 import Control.Applicative
23 import Control.Monad.Trans
24 from Text import class Text(concat), instance Text String
29 //Instruction is an instruction, with possible arguments and a possible comment
33 :: Instr = Instr String [Arg] String
36 :: Arg = L Label | Lit Int | Raw String
37 :: SSMProgram :== [Instr]
38 :: GenError = Error String
39 :: GenMap :== 'Map'.Map String LoadPlace
40 //completely change to either Stack, Heap, Register?
41 :: LoadPlace = LDA Int | LDC Int | LDH Int | LDL Int
44 :: Gen a :== RWST Label SSMProgram (GenMap, [Label]) (Either GenError) a
46 labelStream :: [Label]
47 labelStream = map (\i-> concat ["lab_", toString i]) [1..]
52 expr = (Op2Expr zero (Op1Expr zero UnMinus (IntExpr zero 4)) BiPlus (IntExpr zero 7))
53 expr2 = (FunExpr zero "test" [IntExpr zero 4] [])
54 stmt = (IfStmt (BoolExpr zero True) [] [])
55 prog = case evalRWST (g stmt) "end" ('Map'.newMap, labelStream) of
56 Left (Error e) = abort e
57 Right (_, prog) = toString prog
58 //gen _ = toString [Lab "Test"
59 // ,Instr "ldc" [Lit 1] "Eerste instructie"
60 // ,Instr "ldc" [Lit 2] "Tweede instructie"]
63 //helper functions for the gen monad
66 changeGenMap :: (GenMap -> GenMap) -> Gen GenMap
67 changeGenMap f = modify (appFst f) >>| genMap
69 extend :: String LoadPlace GenMap -> GenMap
70 extend k pl g = 'Map'.put k pl g
73 fresh = gets snd >>= \vars->
74 modify (appSnd $ const $ tail vars) >>|
77 class g a :: a -> Gen ()
80 g (VarExpr _ (VarDef k fs)) = load k >>= \instr-> tell [instr] //note: pure is pure for list, i.e. []
81 g (IntExpr _ i) = loadP (LDC i) >>= \instr-> tell [instr]
82 g (CharExpr _ c) = undef //how to deal with strings?
83 g (BoolExpr _ True) = loadP (LDC TRUE) >>= \instr-> tell [instr]
84 g (BoolExpr _ False) = loadP (LDC FALSE) >>= \instr-> tell [instr]
85 g (Op2Expr _ e1 op e2) = g e1 >>| g e2 >>| tell [Instr (op2ins op) [] ""]
86 g (Op1Expr _ UnNegation e) = g e >>| tell [Instr "not" [] ""]
87 g (Op1Expr _ UnMinus e) = g e >>| tell [Instr "neg" [] ""]
88 g (EmptyListExpr _) = abort "Shit, empty list expr"
89 g (TupleExpr p (e1,e2)) = abort "How to deal with tuples?"
90 g (FunExpr _ k es fs) = mapM g es >>| jump "bsr" k >>= \instr-> tell [instr]
93 g (IfStmt cond th el) =
94 fresh >>= \elseLabel->
97 tell [Instr "brf" [L elseLabel] "branch else"] >>|
99 tell [Instr "bra" [L endLabel] "branch end if"] >>|
100 tell [Lab elseLabel] >>|
103 g (WhileStmt cond th) =
104 fresh >>= \startLabel->
105 fresh >>= \endLabel ->
106 tell [Lab startLabel] >>|
108 tell [Instr "brf" [L endLabel] "branch end while"] >>|
110 tell [Instr "bra" [L startLabel] "branch start while"] >>|
112 g (AssStmt (VarDef k fs) e) =
114 abort "Shit, an assignment, figure out something with storing vars or something"
115 //vars will be on stack in locals (possible pointers to heap)
116 g (FunStmt _ _) = abort "CodeGen, FunStmt unused" //not used
117 g (ReturnStmt Nothing) = tell [Instr "ret" [] ""] //NOTE! Assumes only return address on stack, safe?
118 g (ReturnStmt (Just e)) =
120 tell [Instr "str" [Raw "RR"] ""] >>|
121 g (ReturnStmt Nothing)
123 instance g VarDecl where
124 g _ = abort "How will we store vars? use the locals thing?"
126 instance g FunDecl where
127 g (FunDecl _ k args mt vds stms) =
128 fresh >>= \l-> let lbl = l+++"_"+++k in
130 changeGenMap (extend k (FUNC lbl)) >>|
131 tell [Instr "link" [Lit $ length vds] ""] //reserve room for local variables
132 //Todo: actual code generation. Probably using 'RWST'.local to scope
133 //the VarDecl in this function
139 op2ins :: Op2 -> String
140 op2ins op = case op of
154 BiCons = abort "Shit, Cons, how to deal with this?"
156 load :: String -> Gen Instr
157 load k = genMap >>= \g-> case 'Map'.member k g of
158 False = liftT (Left $ Error $ concat ["PANIC: ", k, " not found in variable mapping"])
159 True = loadP $ 'Map'.find k g
161 loadP :: LoadPlace -> Gen Instr
162 loadP pl = dec pl >>= \(instr, arg)-> pure $ Instr instr [arg] ""
164 dec (LDA i) = pure ("lda", Lit i)
165 dec (LDC i) = pure ("ldc", Lit i)
166 dec (LDH i) = pure ("ldh", Lit i)
167 dec (LDL i) = pure ("ldl", Lit i)
168 dec (LDR i) = pure ("ldr", Lit i)
169 dec (LDS i) = pure ("lds", Lit i)
170 dec _ = liftT (Left $ Error "PANIC: trying to load non adres")
172 //Instruction (String), key of function to jump to
173 jump :: String String -> Gen Instr
174 jump instr k = genMap >>= \g-> case 'Map'.member k g of
175 False = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
176 True = dec ('Map'.find k g) >>= \lbl-> pure $ Instr instr [lbl] (k +++"()")
178 dec (FUNC l) = pure (L l)
179 dec _ = liftT (Left $ Error "PANIC: trying to jump to non label")
181 class print a :: a -> [String]
183 instance print Instr where
184 print (Lab l) = [l, ":", "\n"]
185 print (Instr i args com) = ["\t", i] ++ print args ++ [" ;", com, "\n"]
187 instance print [Arg] where
188 print args = (map toString args)
190 instance toString Arg where
192 toString (Lit int) = toString int
194 instance toString SSMProgram where
195 toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p