NamedBlock.hs 1.17 KB
Newer Older
1 2 3 4
{- sv2v
 - Author: Zachary Snow <zach@zachjs.com>
 -
 - Conversion for unnamed blocks with contain data declarations
5 6
 -
 - SystemVerilog allows data declarations to appear in all blocks, but Verilog
7
 - allows them to appear only in blocks that are named. This conversion gives
8
 - such blocks a unique name to placate strict Verilog frontends.
9 10 11 12
 -}

module Convert.NamedBlock (convert) where

13
import Control.Monad.State.Strict
14 15 16 17

import Convert.Traverse
import Language.SystemVerilog.AST

18
convert :: [AST] -> [AST]
19
convert = map $ traverseDescriptions convertDescription
20

21 22 23 24 25
convertDescription :: Description -> Description
convertDescription description =
    evalState (traverseModuleItemsM traverseModuleItem description) 1
    where
        traverseModuleItem = traverseStmtsM $ traverseNestedStmtsM traverseStmtM
26

27
traverseStmtM :: Stmt -> State Int Stmt
28 29 30
traverseStmtM (Block kw "" [] stmts) =
    return $ Block kw "" [] stmts
traverseStmtM (Block kw "" decls stmts) = do
31
    x <- uniqueBlockName
32
    return $ Block kw x decls stmts
33 34
traverseStmtM other = return other

35 36 37 38 39
uniqueBlockName :: State Int String
uniqueBlockName = do
    cnt <- get
    put $ cnt + 1
    return $ "sv2v_autoblock_" ++ show cnt