Commit 39f6bbb3 by Zachary Snow

preliminary support for extern and module/interface lifetimes

parent 0352414e
...@@ -28,12 +28,12 @@ defaultType :: Type ...@@ -28,12 +28,12 @@ defaultType :: Type
defaultType = IntegerVector TLogic Unspecified [(Number "31", Number "0")] defaultType = IntegerVector TLogic Unspecified [(Number "31", Number "0")]
convertDescription :: Description -> Description convertDescription :: Description -> Description
convertDescription (description @ (Part _ _ _ _)) = convertDescription (description @ (Part _ _ _ _ _ _)) =
Part kw name ports (enumItems ++ items) Part extern kw lifetime name ports (enumItems ++ items)
where where
enumPairs = concat $ map (uncurry enumVals) $ Set.toList enums enumPairs = concat $ map (uncurry enumVals) $ Set.toList enums
enumItems = map (\(x, v) -> MIDecl $ Localparam (Implicit Unspecified []) x v) enumPairs enumItems = map (\(x, v) -> MIDecl $ Localparam (Implicit Unspecified []) x v) enumPairs
(Part kw name ports items, enums) = (Part extern kw lifetime name ports items, enums) =
runWriter $ traverseModuleItemsM (traverseTypesM traverseType) $ runWriter $ traverseModuleItemsM (traverseTypesM traverseType) $
traverseModuleItems (traverseExprs $ traverseNestedExprs traverseExpr) $ traverseModuleItems (traverseExprs $ traverseNestedExprs traverseExpr) $
description description
......
...@@ -25,17 +25,18 @@ convert descriptions = ...@@ -25,17 +25,18 @@ convert descriptions =
descriptions descriptions
where where
interfaces = execWriter $ collectDescriptionsM collectDesc descriptions interfaces = execWriter $ collectDescriptionsM collectDesc descriptions
-- we can only collect/map non-extern interfaces
collectDesc :: Description -> Writer Interfaces () collectDesc :: Description -> Writer Interfaces ()
collectDesc (Part Interface name ports items) = collectDesc (Part False Interface _ name ports items) =
tell $ Map.singleton name (ports, items) tell $ Map.singleton name (ports, items)
collectDesc _ = return () collectDesc _ = return ()
isInterface :: Description -> Bool isInterface :: Description -> Bool
isInterface (Part Interface _ _ _) = True isInterface (Part False Interface _ _ _ _) = True
isInterface _ = False isInterface _ = False
convertDescription :: Interfaces -> Description -> Description convertDescription :: Interfaces -> Description -> Description
convertDescription interfaces (Part Module name ports items) = convertDescription interfaces (Part extern Module lifetime name ports items) =
Part Module name ports' items' Part extern Module lifetime name ports' items'
where where
items' = items' =
map (traverseNestedModuleItems $ traverseExprs (traverseNestedExprs convertExpr)) $ map (traverseNestedModuleItems $ traverseExprs (traverseNestedExprs convertExpr)) $
......
...@@ -25,7 +25,7 @@ convert :: AST -> AST ...@@ -25,7 +25,7 @@ convert :: AST -> AST
convert = traverseDescriptions convertDescription convert = traverseDescriptions convertDescription
convertDescription :: Description -> Description convertDescription :: Description -> Description
convertDescription (orig @ (Part Module _ _ _)) = convertDescription (orig @ (Part _ Module _ _ _ _)) =
traverseModuleItems (traverseDecls convertDecl . convertModuleItem) orig traverseModuleItems (traverseDecls convertDecl . convertModuleItem) orig
where where
idents = execWriter (collectModuleItemsM regIdents orig) idents = execWriter (collectModuleItemsM regIdents orig)
......
...@@ -49,7 +49,7 @@ convert :: AST -> AST ...@@ -49,7 +49,7 @@ convert :: AST -> AST
convert = traverseDescriptions convertDescription convert = traverseDescriptions convertDescription
convertDescription :: Description -> Description convertDescription :: Description -> Description
convertDescription (description @ (Part _ _ ports _)) = convertDescription (description @ (Part _ _ _ _ ports _)) =
hoistPortDecls $ hoistPortDecls $
traverseModuleItems (flattenModuleItem info . rewriteModuleItem info) description traverseModuleItems (flattenModuleItem info . rewriteModuleItem info) description
where where
...@@ -105,8 +105,8 @@ collectNestedLHS _ = return () ...@@ -105,8 +105,8 @@ collectNestedLHS _ = return ()
-- them out with this function. This obviously isn't ideal, but it's a -- them out with this function. This obviously isn't ideal, but it's a
-- relatively straightforward transformation, and testing in VCS is important. -- relatively straightforward transformation, and testing in VCS is important.
hoistPortDecls :: Description -> Description hoistPortDecls :: Description -> Description
hoistPortDecls (Part kw name ports items) = hoistPortDecls (Part extern kw lifetime name ports items) =
Part kw name ports (concat $ map explode items) Part extern kw lifetime name ports (concat $ map explode items)
where where
explode :: ModuleItem -> [ModuleItem] explode :: ModuleItem -> [ModuleItem]
explode (Generate genItems) = explode (Generate genItems) =
......
...@@ -18,7 +18,7 @@ convert descriptions = ...@@ -18,7 +18,7 @@ convert descriptions =
where where
modulePorts = execWriter $ collectDescriptionsM getPorts descriptions modulePorts = execWriter $ collectDescriptionsM getPorts descriptions
getPorts :: Description -> Writer (Map.Map Identifier [Identifier]) () getPorts :: Description -> Writer (Map.Map Identifier [Identifier]) ()
getPorts (Part _ name ports _) = tell $ Map.singleton name ports getPorts (Part _ _ _ name ports _) = tell $ Map.singleton name ports
getPorts _ = return () getPorts _ = return ()
mapInstance :: ModuleItem -> ModuleItem mapInstance :: ModuleItem -> ModuleItem
......
...@@ -80,8 +80,8 @@ maybeDo _ Nothing = return Nothing ...@@ -80,8 +80,8 @@ maybeDo _ Nothing = return Nothing
maybeDo fun (Just val) = fun val >>= return . Just maybeDo fun (Just val) = fun val >>= return . Just
traverseModuleItemsM :: Monad m => MapperM m ModuleItem -> MapperM m Description traverseModuleItemsM :: Monad m => MapperM m ModuleItem -> MapperM m Description
traverseModuleItemsM mapper (Part kw name ports items) = traverseModuleItemsM mapper (Part extern kw lifetime name ports items) =
mapM fullMapper items >>= return . Part kw name ports mapM fullMapper items >>= return . Part extern kw lifetime name ports
where where
fullMapper (Generate genItems) = fullMapper (Generate genItems) =
mapM fullGenItemMapper genItems >>= mapper . Generate mapM fullGenItemMapper genItems >>= mapper . Generate
...@@ -95,8 +95,8 @@ traverseModuleItemsM mapper (Part kw name ports items) = ...@@ -95,8 +95,8 @@ traverseModuleItemsM mapper (Part kw name ports items) =
genItemMapper other = return other genItemMapper other = return other
traverseModuleItemsM mapper (PackageItem packageItem) = do traverseModuleItemsM mapper (PackageItem packageItem) = do
let item = MIPackageItem packageItem let item = MIPackageItem packageItem
Part Module "DNE" [] [item'] <- Part False Module Nothing "DNE" [] [item'] <-
traverseModuleItemsM mapper (Part Module "DNE" [] [item]) traverseModuleItemsM mapper (Part False Module Nothing "DNE" [] [item])
return $ case item' of return $ case item' of
MIPackageItem packageItem' -> PackageItem packageItem' MIPackageItem packageItem' -> PackageItem packageItem'
other -> error $ "encountered bad package module item: " ++ show other other -> error $ "encountered bad package module item: " ++ show other
...@@ -529,8 +529,8 @@ collectAsgnsM = collectify traverseAsgnsM ...@@ -529,8 +529,8 @@ collectAsgnsM = collectify traverseAsgnsM
traverseNestedModuleItemsM :: Monad m => MapperM m ModuleItem -> MapperM m ModuleItem traverseNestedModuleItemsM :: Monad m => MapperM m ModuleItem -> MapperM m ModuleItem
traverseNestedModuleItemsM mapper item = do traverseNestedModuleItemsM mapper item = do
Part Module "DNE" [] [item'] <- Part False Module Nothing "DNE" [] [item'] <-
traverseModuleItemsM mapper (Part Module "DNE" [] [item]) traverseModuleItemsM mapper (Part False Module Nothing "DNE" [] [item])
return item' return item'
traverseNestedModuleItems :: Mapper ModuleItem -> Mapper ModuleItem traverseNestedModuleItems :: Mapper ModuleItem -> Mapper ModuleItem
......
...@@ -82,22 +82,26 @@ instance Show PackageItem where ...@@ -82,22 +82,26 @@ instance Show PackageItem where
show (Comment c) = "// " ++ c show (Comment c) = "// " ++ c
data Description data Description
= Part PartKW Identifier [Identifier] [ModuleItem] = Part Bool PartKW (Maybe Lifetime) Identifier [Identifier] [ModuleItem]
| PackageItem PackageItem | PackageItem PackageItem
| Directive String | Directive String
deriving Eq deriving Eq
instance Show Description where instance Show Description where
showList descriptions _ = intercalate "\n" $ map show descriptions showList descriptions _ = intercalate "\n" $ map show descriptions
show (Part kw name ports items) = unlines show (Part True kw lifetime name _ items) =
[ (show kw) ++ " " ++ name ++ portsStr ++ ";" printf "extern %s %s%s %s;"
, indent $ unlines' $ map show items (show kw) (showLifetime lifetime) name (indentedParenList itemStrs)
, "end" ++ (show kw) ] where itemStrs = map (\(MIDecl a) -> init $ show a) items
show (Part False kw lifetime name ports items) =
printf "%s %s%s%s;\n%s\nend%s"
(show kw) (showLifetime lifetime) name portsStr bodyStr (show kw)
where where
portsStr = portsStr =
if null ports if null ports
then "" then ""
else indentedParenList ports else " " ++ indentedParenList ports
bodyStr = indent $ unlines' $ map show items
show (PackageItem i) = show i show (PackageItem i) = show i
show (Directive str) = str show (Directive str) = str
......
...@@ -98,6 +98,7 @@ tokens :- ...@@ -98,6 +98,7 @@ tokens :-
"endmodule" { tok KW_endmodule } "endmodule" { tok KW_endmodule }
"endtask" { tok KW_endtask } "endtask" { tok KW_endtask }
"enum" { tok KW_enum } "enum" { tok KW_enum }
"extern" { tok KW_extern }
"for" { tok KW_for } "for" { tok KW_for }
"forever" { tok KW_forever } "forever" { tok KW_forever }
"function" { tok KW_function } "function" { tok KW_function }
......
...@@ -43,6 +43,7 @@ import Language.SystemVerilog.Parser.Tokens ...@@ -43,6 +43,7 @@ import Language.SystemVerilog.Parser.Tokens
"endmodule" { Token KW_endmodule _ _ } "endmodule" { Token KW_endmodule _ _ }
"endtask" { Token KW_endtask _ _ } "endtask" { Token KW_endtask _ _ }
"enum" { Token KW_enum _ _ } "enum" { Token KW_enum _ _ }
"extern" { Token KW_extern _ _ }
"for" { Token KW_for _ _ } "for" { Token KW_for _ _ }
"forever" { Token KW_forever _ _ } "forever" { Token KW_forever _ _ }
"function" { Token KW_function _ _ } "function" { Token KW_function _ _ }
...@@ -223,7 +224,8 @@ Descriptions :: { [Description] } ...@@ -223,7 +224,8 @@ Descriptions :: { [Description] }
| Descriptions Description { $1 ++ [$2] } | Descriptions Description { $1 ++ [$2] }
Description :: { Description } Description :: { Description }
: Part { $1 } : Part(ModuleKW , "endmodule" ) { $1 }
| Part(InterfaceKW, "endinterface") { $1 }
| PackageItem { PackageItem $1 } | PackageItem { PackageItem $1 }
| Directive { Directive $1 } | Directive { Directive $1 }
...@@ -296,9 +298,14 @@ Packing :: { Packing } ...@@ -296,9 +298,14 @@ Packing :: { Packing }
| "packed" { Packed Unspecified } | "packed" { Packed Unspecified }
| {- empty -} { Unpacked } | {- empty -} { Unpacked }
Part :: { Description } Part(begin, end) :: { Description }
: "module" Identifier Params PortDecls ";" ModuleItems "endmodule" opt(Tag) { Part Module $2 (fst $4) ($3 ++ (snd $4) ++ $6) } : begin opt(Lifetime) Identifier Params PortDecls ";" ModuleItems end opt(Tag) { Part False $1 $2 $3 (fst $5) ($4 ++ (snd $5) ++ $7) }
| "interface" Identifier Params PortDecls ";" ModuleItems "endinterface" opt(Tag) { Part Interface $2 (fst $4) ($3 ++ (snd $4) ++ $6) } | "extern" begin opt(Lifetime) Identifier Params PortDecls ";" { Part True $2 $3 $4 (fst $6) ($5 ++ (snd $6) ) }
ModuleKW :: { PartKW }
: "module" { Module }
InterfaceKW :: { PartKW }
: "interface" { Interface }
Tag :: { Identifier } Tag :: { Identifier }
: ":" Identifier { $2 } : ":" Identifier { $2 }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment