-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80f615b
commit 626496a
Showing
5 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
lark-parser==0.7.8 | ||
pytest==5.0.1 | ||
pyverilog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""Testing utilities""" | ||
from io import StringIO | ||
|
||
class DiffInfo(object): | ||
def __init__(self, lineno=None, diff_tuples=None, diff_nodes=None): | ||
info = [diff_tuples, diff_nodes] | ||
info = list(filter(lambda x: x is not None, info)) | ||
assert len(info) <= 1, 'At most one type of diff is accepted' | ||
self.diff_tuples = diff_tuples | ||
self.diff_nodes = diff_nodes | ||
self.lineno = lineno | ||
|
||
def __str__(self): | ||
if self.diff_tuples: | ||
res = "# Different Attributes at line {}: ".format(self.lineno) | ||
res = res + '\n'.join("- {}: {}\n+ {}: {}".format(str(k1), str(v1), str(k2), str(v2)) | ||
for k1, v1, k2, v2 in self.diff_tuples) | ||
return res | ||
if self.diff_nodes: | ||
def _get_str(node): | ||
buf = StringIO() | ||
node.show(buf=buf) | ||
return buf.getvalue() | ||
res = "# Different Children at line {}: \n".format(self.lineno) | ||
res += "# In the first file\n" | ||
res += ''.join(map(_get_str, self.diff_nodes[0])) + '\n' | ||
res += "# In the next file\n" | ||
res += ''.join(map(_get_str, self.diff_nodes[1])) | ||
return res | ||
return "" | ||
|
||
class VerilogASTDiff(object): | ||
"""interpret the diff of AST of verilog""" | ||
|
||
@staticmethod | ||
def _diff_traverse(this_node, that_node): | ||
"""traverses two asts using dfs, stops when different node encountered""" | ||
print("at: {}".format(this_node)) | ||
if type(this_node) != type(that_node): | ||
return False | ||
|
||
# assuming each attr_name appear once in each node | ||
this_names = set(this_node.attr_names) | ||
that_names = set(that_node.attr_names) | ||
diff = [] | ||
for a in this_names: | ||
this_attr = getattr(this_node, a) | ||
if a in that_names: | ||
that_attr = getattr(that_node, a) | ||
if this_attr != that_attr: | ||
print(this_attr, that_attr) | ||
diff.append((a, this_attr, a, that_attr)) | ||
else: | ||
diff.append((a, this_attr, '', '')) | ||
for a in that_names: | ||
that_attr = getattr(that_node, a) | ||
if a not in this_names: | ||
diff.append(('', '', a, that_attr)) | ||
|
||
if diff: | ||
return DiffInfo(this_node.lineno, diff_tuples=diff) | ||
|
||
other_children = that_node.children() | ||
|
||
if len(this_node.children()) != len(other_children): | ||
return DiffInfo(this_node.lineno, | ||
diff_nodes = [this_node.children(), | ||
other_children]) | ||
|
||
for i, c in enumerate(this_node.children()): | ||
res = VerilogASTDiff._diff_traverse(c, other_children[i]) | ||
if res: | ||
return res | ||
|
||
return None | ||
|
||
|
||
@staticmethod | ||
def diff_info(this_verilog_ast, that_verilog_ast): | ||
"""shows the diff information of two ast, including line number and node name""" | ||
return VerilogASTDiff._diff_traverse(this_verilog_ast, that_verilog_ast) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Testing utilities""" | ||
import pyverilog.utils.version | ||
from util.vdiff import VerilogASTDiff | ||
from pyverilog.vparser.parser import parse | ||
|
||
class VerilogParser(object): | ||
"""parses Verilog code into AST, useful for comparing verilogs""" | ||
|
||
@staticmethod | ||
def parse(verilog_filename, include_list=None, define_list=None): | ||
""" | ||
parses verilog code | ||
""" | ||
if include_list is None: | ||
include_list = [] | ||
if define_list is None: | ||
define_list = [] | ||
ast, _ = parse([ verilog_filename ], | ||
preprocess_include=include_list, | ||
preprocess_define=define_list) | ||
return ast | ||
|
||
@staticmethod | ||
def diff(this_verilog_filename, that_verilog_filename, | ||
include_list=None, define_list=None): | ||
""" | ||
parses verilog_a and verilog_b and outputs their diff | ||
""" | ||
this_ast = VerilogParser.parse(this_verilog_filename, include_list, define_list) | ||
that_ast = VerilogParser.parse(that_verilog_filename, include_list, define_list) | ||
|
||
this_ast.show() | ||
that_ast.show() | ||
|
||
# show the diff | ||
diff_info = VerilogASTDiff.diff_info(this_ast, that_ast) | ||
return diff_info | ||
|