Cmm back end upgrades
[ghc-hetmet.git] / compiler / cmm / CmmCvt.hs
index ae336b5..3cbd328 100644 (file)
@@ -6,6 +6,7 @@ where
 
 import Cmm
 import CmmExpr
+import MkZipCfg
 import MkZipCfgCmm hiding (CmmGraph)
 import ZipCfgCmmRep -- imported for reverse conversion
 import CmmZipUtil
@@ -14,6 +15,7 @@ import PprCmmZ()
 import qualified ZipCfg as G
 
 import FastString
+import Monad
 import Outputable
 import Panic
 import UniqSet
@@ -24,14 +26,18 @@ import Maybe
 cmmToZgraph :: GenCmm d h (ListGraph CmmStmt) -> UniqSM (GenCmm d h CmmGraph)
 cmmOfZgraph :: GenCmm d h (CmmGraph)          ->         GenCmm d h (ListGraph CmmStmt)
 
-cmmToZgraph = cmmMapGraphM toZgraph
+cmmToZgraph (Cmm tops) = liftM Cmm $ mapM mapTop tops
+  where mapTop (CmmProc h l args g) =
+          toZgraph (showSDoc $ ppr l) args g >>= return . CmmProc h l args
+        mapTop (CmmData s ds) = return $ CmmData s ds
 cmmOfZgraph = cmmMapGraph  ofZgraph
 
 
-toZgraph :: String -> ListGraph CmmStmt -> UniqSM CmmGraph
-toZgraph _ (ListGraph []) = lgraphOfAGraph emptyAGraph
-toZgraph fun_name g@(ListGraph (BasicBlock id ss : other_blocks)) = 
-           labelAGraph id $ mkStmts ss <*> foldr addBlock emptyAGraph other_blocks
+toZgraph :: String -> CmmFormalsWithoutKinds -> ListGraph CmmStmt -> UniqSM CmmGraph
+toZgraph _ _ (ListGraph []) = lgraphOfAGraph emptyAGraph
+toZgraph fun_name args g@(ListGraph (BasicBlock id ss : other_blocks)) = 
+           labelAGraph id $ mkMiddles (mkEntry id undefined args) <*>
+                            mkStmts ss <*> foldr addBlock emptyAGraph other_blocks
   where addBlock (BasicBlock id ss) g = mkLabel id   <*> mkStmts ss <*> g
         mkStmts (CmmNop        : ss)  = mkNop        <*> mkStmts ss 
         mkStmts (CmmComment s  : ss)  = mkComment s  <*> mkStmts ss
@@ -81,13 +87,13 @@ ofZgraph g = ListGraph $ swallow blocks
           tail id prev' out (G.ZTail m t) rest = tail id (mid m : prev') out t rest
           tail id prev' out (G.ZLast G.LastExit)      rest = exit id prev' out rest
           tail id prev' out (G.ZLast (G.LastOther l)) rest = last id prev' out l rest
-          mid (MidNop)        = CmmNop
           mid (MidComment s)  = CmmComment s
           mid (MidAssign l r) = CmmAssign l r
           mid (MidStore  l r) = CmmStore  l r
           mid (MidUnsafeCall f ress args) = CmmCall f ress args CmmUnsafe CmmMayReturn
-          mid m@(CopyOut {})  = pcomment (ppr m)
-          mid m@(CopyIn {})   = pcomment (ppr m <+> text "(proc point)")
+          mid m@(MidAddToContext {}) = pcomment (ppr m)
+          mid m@(CopyOut {})         = pcomment (ppr m)
+          mid m@(CopyIn {})          = pcomment (ppr m <+> text "(proc point)")
           pcomment p = scomment $ showSDoc p
           block' id prev'
               | id == G.lg_entry g = BasicBlock id $ extend_entry    (reverse prev')
@@ -95,15 +101,14 @@ ofZgraph g = ListGraph $ swallow blocks
           last id prev' out l n =
             let endblock stmt = block' id (stmt : prev') : swallow n in
             case l of
-              LastBranch _ (_:_) -> panic "unrepresentable branch"
-              LastBranch tgt [] ->
+              LastBranch tgt ->
                   case n of
                     G.Block id' t : bs
                         | tgt == id', unique_pred id' 
                         -> tail id prev' out t bs -- optimize out redundant labels
                     _ -> if isNothing out then endblock (CmmBranch tgt)
                          else pprPanic "can't convert LGraph with pending CopyOut"
-                                  (ppr g)
+                                  (text "target" <+> ppr tgt <+> ppr g)
               LastCondBranch expr tid fid ->
                 if isJust out then pprPanic "CopyOut before conditional branch" (ppr g)
                 else
@@ -116,8 +121,8 @@ ofZgraph g = ListGraph $ swallow blocks
                                  tail id (CmmCondBranch e'   fid : prev') Nothing t bs
                     _ -> let instrs' = CmmBranch fid : CmmCondBranch expr tid : prev'
                          in block' id instrs' : swallow n
-              LastJump expr params -> endblock $ CmmJump expr params 
-              LastReturn params    -> endblock $ CmmReturn params
+              LastJump expr        -> endblock $ with_out out $ CmmJump expr
+              LastReturn           -> endblock $ with_out out $ CmmReturn 
               LastSwitch arg ids   -> endblock $ CmmSwitch arg $ ids
               LastCall e cont
                   | Just (conv, args) <- out
@@ -137,6 +142,8 @@ ofZgraph g = ListGraph $ swallow blocks
                             in  tail id (delayed : call : prev') Nothing t bs
                          | otherwise -> panic "unrepairable call"
                   | otherwise -> panic "call with no CopyOut"
+          with_out (Just (_conv, actuals)) f = f actuals
+          with_out Nothing f = pprPanic "unrepairable data flow to" (ppr $ f [])
           findCopyIn (G.ZTail (CopyIn _ ress srt) _) = (ress, srt)
           findCopyIn (G.ZTail _ t) = findCopyIn t
           findCopyIn (G.ZLast _) = panic "missing CopyIn after call"
@@ -155,13 +162,13 @@ ofZgraph g = ListGraph $ swallow blocks
           single_preds =
               let add b single =
                     let id = G.blockId b
-                    in  case G.lookupBlockEnv preds id of
+                    in  case lookupBlockEnv preds id of
                           Nothing -> single
                           Just s -> if sizeUniqSet s == 1 then
-                                        G.extendBlockSet single id
+                                        extendBlockSet single id
                                     else single
-              in  G.fold_blocks add G.emptyBlockSet g
-          unique_pred id = G.elemBlockSet id single_preds
+              in  G.fold_blocks add emptyBlockSet g
+          unique_pred id = elemBlockSet id single_preds
           call_succs = 
               let add b succs =
                       case G.last (G.unzip b) of