codegen work in progress
authorpimjager <pim@pimjager.nl>
Sun, 8 May 2016 22:43:56 +0000 (00:43 +0200)
committerpimjager <pim@pimjager.nl>
Sun, 8 May 2016 22:43:56 +0000 (00:43 +0200)
gen.icl

diff --git a/gen.icl b/gen.icl
index be2c3ce..e1f6838 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -63,6 +63,11 @@ gen _ = prog
 //helper functions for the gen monad
 genMap :: Gen GenMap
 genMap = gets fst
+changeGenMap :: (GenMap -> GenMap) -> Gen GenMap
+changeGenMap f = modify (appFst f) >>| genMap
+
+extend :: String LoadPlace GenMap -> GenMap
+extend k pl g = 'Map'.put k pl g 
 
 fresh :: Gen Label
 fresh = gets snd >>= \vars-> 
@@ -83,7 +88,6 @@ instance g Expr where
     g (EmptyListExpr _) = abort "Shit, empty list expr"
     g (TupleExpr p (e1,e2)) = abort "How to deal with tuples?"
     g (FunExpr _ k es fs) = mapM g es >>| jump "bsr" k >>= \instr-> tell [instr]
-            //bra is probably not right, figure out function call way
 
 instance g Stmt where
     g (IfStmt cond th el) = 
@@ -108,6 +112,7 @@ instance g Stmt where
     g (AssStmt (VarDef k fs) e) = 
         g e >>|
         abort "Shit, an assignment, figure out something with storing vars or something"
+        //vars will be on stack in locals (possible pointers to heap)
     g (FunStmt _ _) = abort "CodeGen FunStmt unused" //not used
     g (ReturnStmt Nothing)  = tell [Instr "ret" [] ""]
     g (ReturnStmt (Just e)) = 
@@ -115,6 +120,20 @@ instance g Stmt where
         tell [Instr "str" [Raw "RR"] ""] >>|
         g (ReturnStmt Nothing)
 
+instance g VarDecl where
+    g _ = abort "How will we store vars? use the locals thing?"
+
+instance g FunDecl where
+    g (FunDecl _ k args mt vds stms) = 
+        fresh >>= \l-> let lbl = l+++"_"+++k in 
+        tell [Lab lbl] >>|
+        changeGenMap (extend k (FUNC lbl)) >>| 
+        tell [Instr "link" [Lit $ length vds] ""] //reserve room for local variables
+        //Todo: actual code generation. Probably using 'RWST'.local to scope 
+        //the VarDecl in this function
+
+
+
 
 
 op2ins :: Op2 -> String
@@ -154,7 +173,7 @@ where
 jump :: String String -> Gen Instr
 jump instr k = genMap >>= \g-> case 'Map'.member k g of
     False = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
-    True = dec ('Map'.find k g) >>= \lbl-> pure $ Instr instr [lbl] k 
+    True = dec ('Map'.find k g) >>= \lbl-> pure $ Instr instr [lbl] (k +++"()") 
 where
     dec (FUNC l) = pure (L l)
     dec _ = liftT (Left $ Error "PANIC: trying to jump to non label")