mirror of https://github.com/zachjs/sv2v.git
preliminary support for enums
This commit is contained in:
parent
b81341c0ac
commit
e795109f5f
|
|
@ -116,10 +116,11 @@ unflattener outputs (arr, (t, (majorHi, majorLo))) =
|
||||||
, Ident startBit )
|
, Ident startBit )
|
||||||
|
|
||||||
typeDims :: Type -> ([Range] -> Type, [Range])
|
typeDims :: Type -> ([Range] -> Type, [Range])
|
||||||
typeDims (Reg r) = (Reg , r)
|
typeDims (Reg r) = (Reg , r)
|
||||||
typeDims (Wire r) = (Wire , r)
|
typeDims (Wire r) = (Wire , r)
|
||||||
typeDims (Logic r) = (Logic , r)
|
typeDims (Logic r) = (Logic , r)
|
||||||
typeDims (Alias t r) = (Alias t, r)
|
typeDims (Alias t r) = (Alias t, r)
|
||||||
|
typeDims (Enum t v r) = (Enum t v, r)
|
||||||
|
|
||||||
prefix :: Identifier -> Identifier
|
prefix :: Identifier -> Identifier
|
||||||
prefix ident = "_sv2v_" ++ ident
|
prefix ident = "_sv2v_" ++ ident
|
||||||
|
|
@ -168,6 +169,7 @@ rewriteExpr dimMap = rewriteExpr'
|
||||||
rewriteExpr' (BinOp o e1 e2) = BinOp o (re e1) (re e2)
|
rewriteExpr' (BinOp o e1 e2) = BinOp o (re e1) (re e2)
|
||||||
rewriteExpr' (Mux e1 e2 e3) = Mux (re e1) (re e2) (re e3)
|
rewriteExpr' (Mux e1 e2 e3) = Mux (re e1) (re e2) (re e3)
|
||||||
rewriteExpr' (Bit e n) = Bit (re e) n
|
rewriteExpr' (Bit e n) = Bit (re e) n
|
||||||
|
rewriteExpr' (Cast t e) = Cast t (re e)
|
||||||
|
|
||||||
flattenRanges :: [Range] -> [Range]
|
flattenRanges :: [Range] -> [Range]
|
||||||
flattenRanges rs =
|
flattenRanges rs =
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@
|
||||||
-- TODO: Right now we only support typedefs for module data items. Function
|
-- TODO: Right now we only support typedefs for module data items. Function
|
||||||
-- parameters, block items, etc., probably support typedefs, too.
|
-- parameters, block items, etc., probably support typedefs, too.
|
||||||
|
|
||||||
|
-- TODO FIXME XXX: `Cast` contains a type, which we'll need to resolve/convert?
|
||||||
|
|
||||||
module Convert.Typedef (convert) where
|
module Convert.Typedef (convert) where
|
||||||
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
|
@ -38,12 +40,15 @@ resolveType :: Types -> Type -> Type
|
||||||
resolveType _ (Reg rs) = Reg rs
|
resolveType _ (Reg rs) = Reg rs
|
||||||
resolveType _ (Wire rs) = Wire rs
|
resolveType _ (Wire rs) = Wire rs
|
||||||
resolveType _ (Logic rs) = Logic rs
|
resolveType _ (Logic rs) = Logic rs
|
||||||
|
resolveType _ (Enum Nothing vals rs) = Enum Nothing vals rs
|
||||||
|
resolveType types (Enum (Just t) vals rs) = Enum (Just $ resolveType types t) vals rs
|
||||||
resolveType types (Alias st rs1) =
|
resolveType types (Alias st rs1) =
|
||||||
case resolveType types $ types Map.! st of
|
case resolveType types $ types Map.! st of
|
||||||
(Reg rs2) -> Reg $ rs2 ++ rs1
|
(Reg rs2) -> Reg $ rs2 ++ rs1
|
||||||
(Wire rs2) -> Wire $ rs2 ++ rs1
|
(Wire rs2) -> Wire $ rs2 ++ rs1
|
||||||
(Logic rs2) -> Logic $ rs2 ++ rs1
|
(Logic rs2) -> Logic $ rs2 ++ rs1
|
||||||
(Alias _ _) -> error $ "resolveType invariant failed on " ++ st
|
(Enum t v rs2) -> Enum t v $ rs2 ++ rs1
|
||||||
|
(Alias _ _) -> error $ "resolveType invariant failed on " ++ st
|
||||||
|
|
||||||
convertModuleItem :: Types -> ModuleItem -> ModuleItem
|
convertModuleItem :: Types -> ModuleItem -> ModuleItem
|
||||||
convertModuleItem types (LocalNet t ident val) =
|
convertModuleItem types (LocalNet t ident val) =
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,7 @@ data Type
|
||||||
| Wire [Range]
|
| Wire [Range]
|
||||||
| Logic [Range]
|
| Logic [Range]
|
||||||
| Alias String [Range]
|
| Alias String [Range]
|
||||||
|
| Enum (Maybe Type) [(Identifier, Maybe Expr)] [Range]
|
||||||
deriving Eq
|
deriving Eq
|
||||||
|
|
||||||
instance Show Type where
|
instance Show Type where
|
||||||
|
|
@ -84,6 +85,13 @@ instance Show Type where
|
||||||
show (Wire r) = "wire" ++ (showRanges r)
|
show (Wire r) = "wire" ++ (showRanges r)
|
||||||
show (Logic r) = "logic" ++ (showRanges r)
|
show (Logic r) = "logic" ++ (showRanges r)
|
||||||
show (Alias t r) = t ++ (showRanges r)
|
show (Alias t r) = t ++ (showRanges r)
|
||||||
|
show (Enum mt vals r) = printf "enum %s{%s}%s" tStr (commas $ map showVal vals) (showRanges r)
|
||||||
|
where
|
||||||
|
tStr = case mt of
|
||||||
|
Nothing -> ""
|
||||||
|
Just t -> (show t) ++ " "
|
||||||
|
showVal :: (Identifier, Maybe Expr) -> String
|
||||||
|
showVal (x, e) = x ++ (showAssignment e)
|
||||||
|
|
||||||
data ModuleItem
|
data ModuleItem
|
||||||
= Comment String
|
= Comment String
|
||||||
|
|
@ -207,6 +215,7 @@ data Expr
|
||||||
| BinOp BinOp Expr Expr
|
| BinOp BinOp Expr Expr
|
||||||
| Mux Expr Expr Expr
|
| Mux Expr Expr Expr
|
||||||
| Bit Expr Int
|
| Bit Expr Int
|
||||||
|
| Cast Type Expr
|
||||||
deriving Eq
|
deriving Eq
|
||||||
|
|
||||||
data UniOp
|
data UniOp
|
||||||
|
|
@ -295,6 +304,7 @@ instance Show Expr where
|
||||||
BinOp a b c -> printf "(%s %s %s)" (show b) (show a) (show c)
|
BinOp a b c -> printf "(%s %s %s)" (show b) (show a) (show c)
|
||||||
Mux a b c -> printf "(%s ? %s : %s)" (show a) (show b) (show c)
|
Mux a b c -> printf "(%s ? %s : %s)" (show a) (show b) (show c)
|
||||||
Bit a b -> printf "(%s [%d])" (show a) b
|
Bit a b -> printf "(%s [%d])" (show a) b
|
||||||
|
Cast a b -> printf "%s'(%s)" (show a) (show b)
|
||||||
|
|
||||||
data LHS
|
data LHS
|
||||||
= LHS Identifier
|
= LHS Identifier
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ tokens :-
|
||||||
"endmodule" { tok KW_endmodule }
|
"endmodule" { tok KW_endmodule }
|
||||||
"endfunction" { tok KW_endfunction}
|
"endfunction" { tok KW_endfunction}
|
||||||
"endgenerate" { tok KW_endgenerate}
|
"endgenerate" { tok KW_endgenerate}
|
||||||
|
"enum" { tok KW_enum }
|
||||||
"function" { tok KW_function }
|
"function" { tok KW_function }
|
||||||
"for" { tok KW_for }
|
"for" { tok KW_for }
|
||||||
"generate" { tok KW_generate }
|
"generate" { tok KW_generate }
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ import Language.SystemVerilog.Parser.Tokens
|
||||||
"endfunction" { Token KW_endfunction _ _ }
|
"endfunction" { Token KW_endfunction _ _ }
|
||||||
"endgenerate" { Token KW_endgenerate _ _ }
|
"endgenerate" { Token KW_endgenerate _ _ }
|
||||||
"endmodule" { Token KW_endmodule _ _ }
|
"endmodule" { Token KW_endmodule _ _ }
|
||||||
|
"enum" { Token KW_enum _ _ }
|
||||||
"function" { Token KW_function _ _ }
|
"function" { Token KW_function _ _ }
|
||||||
"for" { Token KW_for _ _ }
|
"for" { Token KW_for _ _ }
|
||||||
"generate" { Token KW_generate _ _ }
|
"generate" { Token KW_generate _ _ }
|
||||||
|
|
@ -178,10 +179,13 @@ Description :: { Description }
|
||||||
Typedef :: { Description }
|
Typedef :: { Description }
|
||||||
: "typedef" Type Identifier ";" { Typedef $2 $3 }
|
: "typedef" Type Identifier ";" { Typedef $2 $3 }
|
||||||
|
|
||||||
|
TypeNonAlias :: { Type }
|
||||||
|
: "wire" Dimensions { Wire $2 }
|
||||||
|
| "reg" Dimensions { Reg $2 }
|
||||||
|
| "logic" Dimensions { Logic $2 }
|
||||||
|
| "enum" opt(Type) "{" VariablePortIdentifiers "}" Dimensions { Enum $2 $4 $6 }
|
||||||
Type :: { Type }
|
Type :: { Type }
|
||||||
: "wire" Dimensions { Wire $2 }
|
: TypeNonAlias { $1 }
|
||||||
| "reg" Dimensions { Reg $2 }
|
|
||||||
| "logic" Dimensions { Logic $2 }
|
|
||||||
| Identifier Dimensions { Alias $1 $2 }
|
| Identifier Dimensions { Alias $1 $2 }
|
||||||
|
|
||||||
Module :: { Description }
|
Module :: { Description }
|
||||||
|
|
@ -242,10 +246,9 @@ ModuleItems :: { [ModuleItem] }
|
||||||
ModuleItem :: { [ModuleItem] }
|
ModuleItem :: { [ModuleItem] }
|
||||||
: PortDecl(";") { $1 }
|
: PortDecl(";") { $1 }
|
||||||
-- TODO: Allowing Ranges on aliases creates conflicts
|
-- TODO: Allowing Ranges on aliases creates conflicts
|
||||||
| Identifier VariableIdentifiers ";" { map (uncurry $ LocalNet (Alias $1 [])) $2 }
|
| Identifier VariableIdentifiers ";" { map (uncurry $ LocalNet (Alias $1 [])) $2 }
|
||||||
| "wire" Dimensions VariableIdentifiers ";" { map (uncurry $ LocalNet $ Wire $2) $3 }
|
| Identifier DimensionsNonEmpty VariableIdentifiers ";" { map (uncurry $ LocalNet (Alias $1 $2)) $3 }
|
||||||
| "reg" Dimensions VariableIdentifiers ";" { map (uncurry $ LocalNet $ Reg $2) $3 }
|
| TypeNonAlias VariableIdentifiers ";" { map (uncurry $ LocalNet $1) $2 }
|
||||||
| "logic" Dimensions VariableIdentifiers ";" { map (uncurry $ LocalNet $ Logic $2) $3 }
|
|
||||||
| ParameterDeclaration { map MIParameter $1 }
|
| ParameterDeclaration { map MIParameter $1 }
|
||||||
| LocalparamDeclaration { map MILocalparam $1 }
|
| LocalparamDeclaration { map MILocalparam $1 }
|
||||||
| IntegerDeclaration { map MIIntegerV $1 }
|
| IntegerDeclaration { map MIIntegerV $1 }
|
||||||
|
|
@ -473,6 +476,7 @@ Expr :: { Expr }
|
||||||
| "^" Expr %prec RedOps { UniOp RedXor $2 }
|
| "^" Expr %prec RedOps { UniOp RedXor $2 }
|
||||||
| "~^" Expr %prec RedOps { UniOp RedXnor $2 }
|
| "~^" Expr %prec RedOps { UniOp RedXnor $2 }
|
||||||
| "^~" Expr %prec RedOps { UniOp RedXnor $2 }
|
| "^~" Expr %prec RedOps { UniOp RedXnor $2 }
|
||||||
|
| Type "'" "(" Expr ")" { Cast $1 $4 }
|
||||||
|
|
||||||
GenItemOrNull :: { GenItem }
|
GenItemOrNull :: { GenItem }
|
||||||
: GenItem { $1 }
|
: GenItem { $1 }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue