Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sv2v
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
sv2v
Commits
8e1693d3
Commit
8e1693d3
authored
Jul 25, 2020
by
Zachary Snow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
yosys-compatible unbased unsized binding conversion
parent
21ebbb5a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
105 additions
and
87 deletions
+105
-87
src/Convert/UnbasedUnsized.hs
+105
-87
No files found.
src/Convert/UnbasedUnsized.hs
View file @
8e1693d3
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TupleSections #-}
{- sv2v
{- sv2v
- Author: Zachary Snow <zach@zachjs.com>
- Author: Zachary Snow <zach@zachjs.com>
-
-
...
@@ -6,116 +7,133 @@
...
@@ -6,116 +7,133 @@
-
-
- The literals are given a binary base, a size of 1, and are made signed to
- The literals are given a binary base, a size of 1, and are made signed to
- allow sign extension. For context-determined expressions, the converted
- allow sign extension. For context-determined expressions, the converted
- literals are
explicitly cast to the appropriat
e context-determined size.
- literals are
repeated to match th
e context-determined size.
-
-
- As a special case, unbased, unsized literals which take on the size of a
- As a special case, unbased, unsized literals which take on the size of a
- module port binding are replaced with a hierarchical reference to an
- module's port are replaced as above, but with the size of the port being
- appropriately sized constant which is injected into the instantiated module's
- determined based on the parameter bindings of the instance and the definition
- definition. This allows these literals to be used for parameterized ports
- of the instantiated module.
- without further complicating other conversions, as hierarchical references
- are not allowed within constant expressions.
-}
-}
module
Convert.UnbasedUnsized
(
convert
)
where
module
Convert.UnbasedUnsized
(
convert
)
where
import
Control.Monad.Writer
import
Control.Monad.Writer
import
Data.Maybe
(
catMaybes
)
import
qualified
Data.Map.Strict
as
Map
import
Convert.ExprUtils
import
Convert.Traverse
import
Convert.Traverse
import
Language.SystemVerilog.AST
import
Language.SystemVerilog.AST
type
Part
=
([
Identifier
],
[
ModuleItem
])
type
Parts
=
Map
.
Map
Identifier
Part
data
ExprContext
data
ExprContext
=
SelfDetermined
=
SelfDetermined
|
ContextDetermined
Expr
|
ContextDetermined
Expr
deriving
(
Eq
,
Show
)
deriving
(
Eq
,
Show
)
type
Port
=
Either
Identifier
Int
data
Bind
=
Bind
{
bModule
::
Identifier
,
bBit
::
Char
,
bPort
::
Port
}
deriving
(
Eq
,
Show
)
type
Binds
=
[
Bind
]
convert
::
[
AST
]
->
[
AST
]
convert
::
[
AST
]
->
[
AST
]
convert
files
=
convert
files
=
map
(
traverseDescriptions
$
convertDescription
binds
)
files'
map
(
traverseDescriptions
convertDescription
)
files
where
where
(
files'
,
binds
)
=
runWriter
$
parts
=
execWriter
$
mapM
(
collectDescriptionsM
collectPartsM
)
files
mapM
(
mapM
$
traverseModuleItemsM
convertModuleItemM
)
file
s
convertDescription
=
traverseModuleItems
$
convertModuleItem
part
s
convertDescription
::
Binds
->
Description
->
Description
collectPartsM
::
Description
->
Writer
Parts
()
convertDescription
[]
other
=
other
collectPartsM
(
Part
_
_
_
_
name
ports
items
)
=
convertDescription
binds
(
Part
attrs
extern
kw
lifetime
name
ports
items
)
=
tell
$
Map
.
singleton
name
(
ports
,
items
)
Part
attrs
extern
kw
lifetime
name
ports
items'
collectPartsM
_
=
return
()
convertModuleItem
::
Parts
->
ModuleItem
->
ModuleItem
convertModuleItem
parts
(
Instance
moduleName
params
instanceName
[]
bindings
)
=
convertModuleItem'
$
Instance
moduleName
params
instanceName
[]
bindings'
where
where
binds'
=
filter
((
==
name
)
.
bModule
)
binds
bindings'
=
zipWith
convertBinding
bindings
[
0
..
]
items'
=
removeDupes
[]
$
items
++
map
(
bindItem
ports
)
binds'
(
portNames
,
moduleItems
)
=
removeDupes
::
[
Identifier
]
->
[
ModuleItem
]
->
[
ModuleItem
]
case
Map
.
lookup
moduleName
parts
of
removeDupes
_
[]
=
[]
Nothing
->
error
$
"could not find module: "
++
moduleName
removeDupes
existing
(
item
@
(
MIPackageItem
(
Decl
decl
))
:
is
)
=
Just
partInfo
->
partInfo
case
decl
of
tag
=
Ident
"~~uub~~"
Param
Localparam
_
x
_
->
convertBinding
::
PortBinding
->
Int
->
PortBinding
if
elem
x
existing
convertBinding
(
portName
,
expr
)
idx
=
then
removeDupes
existing
is
(
portName
,
)
$
else
item
:
removeDupes
(
x
:
existing
)
is
traverseNestedExprs
(
replaceBindingExpr
portName
idx
)
$
_
->
item
:
removeDupes
existing
is
convertExpr
(
ContextDetermined
tag
)
expr
removeDupes
existing
(
item
:
is
)
=
replaceBindingExpr
::
Identifier
->
Int
->
Expr
->
Expr
item
:
removeDupes
existing
is
replaceBindingExpr
portName
idx
(
orig
@
(
Repeat
_
[
ConvertedUU
a
b
]))
=
convertDescription
_
other
=
other
if
orig
==
sizedLiteralFor
tag
ch
then
Repeat
portSize
[
ConvertedUU
a
b
]
bindName
::
Bind
->
Identifier
else
orig
bindName
(
Bind
_
ch
(
Left
x
))
=
"sv2v_uub_"
++
ch
:
'_'
:
x
where
bindName
(
Bind
m
ch
(
Right
i
))
=
ch
=
charForBit
a
b
bindName
$
Bind
m
ch
(
Left
$
show
i
)
portName'
=
if
null
portName
bindItem
::
[
Identifier
]
->
Bind
->
ModuleItem
then
lookupBindingName
portNames
idx
bindItem
ports
bind
=
else
portName
MIPackageItem
$
Decl
$
Param
Localparam
typ
name
expr
portSize
=
determinePortSize
portName'
params
moduleItems
replaceBindingExpr
_
_
other
=
other
convertModuleItem
_
other
=
convertModuleItem'
other
determinePortSize
::
Identifier
->
[
ParamBinding
]
->
[
ModuleItem
]
->
Expr
determinePortSize
portName
instanceParams
moduleItems
=
step
initialMapping
moduleItems
where
where
portName
=
lookupPort
ports
(
bPort
bind
)
moduleParams
=
parameterNames
moduleItems
size
=
DimsFn
FnBits
$
Right
$
Ident
portName
initialMapping
=
catMaybes
$
rng
=
(
BinOp
Sub
size
(
RawNum
1
),
RawNum
0
)
zipWith
createParamReplacement
instanceParams
[
0
..
]
typ
=
Implicit
Unspecified
[
rng
]
createParamReplacement
name
=
bindName
bind
::
ParamBinding
->
Int
->
Maybe
(
Identifier
,
Expr
)
expr
=
literalFor
$
bBit
bind
createParamReplacement
(
""
,
b
)
idx
=
createParamReplacement
(
paramName
,
b
)
idx
lookupPort
::
[
Identifier
]
->
Port
->
Identifier
where
paramName
=
lookupBindingName
moduleParams
idx
lookupPort
_
(
Left
x
)
=
x
createParamReplacement
(
_
,
Left
_
)
_
=
Nothing
lookupPort
ports
(
Right
i
)
=
createParamReplacement
(
paramName
,
Right
expr
)
_
=
if
i
<
length
ports
Just
(
paramName
,
tagExpr
expr
)
then
ports
!!
i
else
error
$
"out of bounds bort binding "
++
show
(
ports
,
i
)
step
::
[(
Identifier
,
Expr
)]
->
[
ModuleItem
]
->
Expr
step
mapping
(
MIPackageItem
(
Decl
(
Param
_
_
x
e
))
:
rest
)
=
convertModuleItemM
::
ModuleItem
->
Writer
Binds
ModuleItem
step
(
mapping
++
[(
x
,
e
)])
rest
convertModuleItemM
(
Instance
moduleName
params
instanceName
[]
bindings
)
=
do
step
mapping
(
MIPackageItem
(
Decl
(
Variable
_
t
x
a
_
))
:
rest
)
=
bindings'
<-
mapM
(
uncurry
convertBinding
)
$
zip
bindings
[
0
..
]
if
x
==
portName
let
item
=
Instance
moduleName
params
instanceName
[]
bindings'
then
substituteExpr
mapping
size
return
$
convertModuleItem
item
else
step
mapping
rest
where
size
=
BinOp
Mul
(
dimensionsSize
a
)
(
DimsFn
FnBits
$
Left
t
)
step
mapping
(
_
:
rest
)
=
step
mapping
rest
step
_
[]
=
error
$
"could not find size of port "
++
portName
substituteExpr
::
[(
Identifier
,
Expr
)]
->
Expr
->
Expr
substituteExpr
_
(
Ident
(
':'
:
x
))
=
Ident
x
substituteExpr
mapping
(
Ident
x
)
=
case
lookup
x
mapping
of
Nothing
->
Ident
x
Just
expr
->
substituteExpr
mapping
expr
substituteExpr
mapping
expr
=
traverseSinglyNestedExprs
(
substituteExpr
mapping
)
expr
tagExpr
::
Expr
->
Expr
tagExpr
(
Ident
x
)
=
Ident
(
':'
:
x
)
tagExpr
expr
=
traverseSinglyNestedExprs
tagExpr
expr
-- given a list of module items, produces the parameter names in order
parameterNames
::
[
ModuleItem
]
->
[
Identifier
]
parameterNames
=
execWriter
.
mapM
(
collectNestedModuleItemsM
$
collectDeclsM
collectDeclM
)
where
where
tag
=
Ident
":uub:"
collectDeclM
::
Decl
->
Writer
[
Identifier
]
()
convertBinding
::
PortBinding
->
Int
->
Writer
Binds
PortBinding
collectDeclM
(
Param
Parameter
_
x
_
)
=
tell
[
x
]
convertBinding
(
portName
,
expr
)
idx
=
do
collectDeclM
(
ParamType
Parameter
x
_
)
=
tell
[
x
]
let
port
=
if
null
portName
then
Right
idx
else
Left
portName
collectDeclM
_
=
return
()
let
expr'
=
convertExpr
(
ContextDetermined
tag
)
expr
expr''
<-
traverseNestedExprsM
(
replaceBindingExpr
port
)
expr'
lookupBindingName
::
[
Identifier
]
->
Int
->
Identifier
return
(
portName
,
expr''
)
lookupBindingName
names
idx
=
replaceBindingExpr
::
Port
->
Expr
->
Writer
Binds
Expr
if
idx
<
length
names
replaceBindingExpr
port
(
orig
@
(
Repeat
_
[
ConvertedUU
a
b
]))
=
do
then
names
!!
idx
let
ch
=
charForBit
a
b
else
error
$
"out of bounds binding "
++
show
(
names
,
idx
)
if
orig
==
sizedLiteralFor
tag
ch
then
do
convertModuleItem'
::
ModuleItem
->
ModuleItem
let
bind
=
Bind
moduleName
ch
port
convertModuleItem'
=
tell
[
bind
]
let
expr
=
Dot
(
Ident
instanceName
)
(
bindName
bind
)
return
expr
else
return
orig
replaceBindingExpr
_
other
=
return
other
convertModuleItemM
other
=
return
$
convertModuleItem
other
convertModuleItem
::
ModuleItem
->
ModuleItem
convertModuleItem
=
traverseExprs
(
convertExpr
SelfDetermined
)
.
traverseExprs
(
convertExpr
SelfDetermined
)
.
traverseTypes
(
traverseNestedTypes
convertType
)
.
traverseTypes
(
traverseNestedTypes
convertType
)
.
traverseAsgns
convertAsgn
traverseAsgns
convertAsgn
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment