Skip to content

Commit

Permalink
make bbid always increasing, fix dot plot edge dir
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjunphy committed Jul 4, 2024
1 parent eaa23d7 commit b655107
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
20 changes: 20 additions & 0 deletions src/CFG/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,29 @@ buildMethod decl@AST.MethodDecl {sig = sig, block = block@(AST.Block _ stmts sid
updateCFG $ do
G.addEdge entry blockH SeqEdge
G.addEdge blockT exit SeqEdge
replaceExitBlock exit
#cfg . _Just . #arguments .= vars
getCFG

-- Replace previously created exit block so id's are always monotonically increasing.
replaceExitBlock :: BBID -> CFGBuild ()
replaceExitBlock prevExit = do
exit <- createEmptyBB
g' <- use #cfg
case g' of
Nothing -> return ()
Just g -> do
let edges = G.edgeToList $ g ^. #graph
let nodes = G.nodeToList $ g ^. #graph
updateCFG $ forM_ edges (updateExitEdge exit)
where
updateExitEdge exit (src, dst, ed) = do
when (src == prevExit)
(G.deleteEdge src dst >> G.addEdge exit dst ed)
when (dst == prevExit)
(G.deleteEdge src dst >> G.addEdge src exit ed)


buildBlock :: [AST.Argument] -> AST.Block -> CFGBuild (BBID, BBID, [Var])
buildBlock args block@AST.Block {stmts = stmts} = do
checkStmts
Expand Down
18 changes: 9 additions & 9 deletions src/CFG/Plot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Lazy qualified as LT
import Formatting
import SSA qualified
import Semantic qualified as SE
import Types
import Util.Graph qualified as G
import SSA qualified

-- Reorder some backward edges introduced by loops so graphviz could find a
-- clear ordering of the nodes.
findBackEdges :: CFG -> Set (BBID, BBID)
findBackEdges (CFG g _ exit _ _) =
findBackEdges (CFG g _ _ _ _) =
Set.fromList $
filter (\(b1, b2) -> b1 > b2 && b2 /= exit) $
filter (uncurry (>)) $
fmap (\(b1, b2, _) -> (b1, b2)) (G.edgeToList g)

data GVizNode = GVizNode
Expand Down Expand Up @@ -135,12 +135,12 @@ cfgToSubgraph cfg = GVizSubgraph name nodes edges
nodes = G.nodeToList graph <&> basicBlockToNode (Just cfg) . snd
backEdges = findBackEdges cfg
isBackEdge (from, to, _) = Set.member (from, to) backEdges
convertEdge edge'@(from, to, edge) =
GVizEdge
from
to
(prettyPrintEdge edge)
(if isBackEdge edge' then "back" else "forward")
convertEdge edge@(from, to, ed) =
let (from', to', dir) =
if isBackEdge edge
then (to, from, "back")
else (from, to, "forward")
in GVizEdge from' to' (prettyPrintEdge ed) dir
edges = G.edgeToList graph <&> convertEdge

fileCFGsToGraph :: SingleFileCFG -> GVizGraph
Expand Down
31 changes: 24 additions & 7 deletions src/CodeGen/LLVMGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@

module CodeGen.LLVMGen where

import AST qualified
import CFG (CFG (..), SingleFileCFG (..))
import CFG qualified
import CodeGen.LLVMIR
import Control.Lens ((^.))
import Control.Monad.Except
import Control.Monad.State
import SSA (SSA)
import SSA qualified
import Types (CompileError (CompileError))
import Control.Lens ((^.))
import Data.Functor ((<&>))
import Data.Generics.Labels
import Data.Text (Text)
import Data.Text qualified as Text
import AST qualified
import SSA (SSA)
import SSA qualified
import Types (BBID, CompileError (CompileError))
import Util.Graph qualified as G

data LLVMGenState = LLVMGenState

Expand All @@ -52,11 +54,26 @@ convertType (AST.ArrayType tpe len) = ArrayType (convertType tpe) (fromIntegral
convertType (AST.Ptr tpe) = PointerType (convertType tpe)

genLLVMIR :: SingleFileCFG -> LLVMGen Module
genLLVMIR (SingleFileCFG global cfgs) = undefined
genLLVMIR (SingleFileCFG global cfgs) = do
globals <- genGlobalBB global
undefined

genGlobalBB :: CFG.BasicBlock -> LLVMGen [Global]
genGlobalBB (CFG.BasicBlock _ _ insts) = forM insts genGlobalDecl
genGlobalBB (CFG.BasicBlock _ _ insts) = forM insts genGlobalDecl

genGlobalDecl :: SSA -> LLVMGen Global
genGlobalDecl (SSA.InitGlobal dst tpe) = return $ Global (varName dst) (convertType tpe)
genGlobalDecl _ = throwError $ CompileError Nothing "Global basic block contains instruction other than alloc."

genFunction :: CFG -> LLVMGen Function
genFunction (CFG g _ _ args sig) = do
let name = sig ^. #name
let arguments = args <&> genArgument
let bbs = genBasicBlocks g
return $ Function name arguments bbs

genArgument :: SSA.Var -> Argument
genArgument var = Argument (varName var) (convertType $ var ^. #tpe)

genBasicBlocks :: G.Graph BBID CFG.BasicBlock CFG.CFGEdge -> [BasicBlock]
genBasicBlocks g = undefined

0 comments on commit b655107

Please sign in to comment.