Add yapf and reformat python code

This commit is contained in:
Wilson Snyder 2020-12-18 22:34:14 -05:00
parent a16ebaf79c
commit ec4e408b2b
6 changed files with 125 additions and 77 deletions

View File

@ -461,6 +461,8 @@ analyzer-include:
-rm -rf examples/*/obj* -rm -rf examples/*/obj*
scan-build $(MAKE) -k examples scan-build $(MAKE) -k examples
format: clang-format yapf
CLANGFORMAT = clang-format CLANGFORMAT = clang-format
CLANGFORMAT_FLAGS = -i CLANGFORMAT_FLAGS = -i
CLANGFORMAT_FILES = $(CPPCHECK_CPP) $(CPPCHECK_H) $(CPPCHECK_YL) test_regress/t/*.c* test_regress/t/*.h CLANGFORMAT_FILES = $(CPPCHECK_CPP) $(CPPCHECK_H) $(CPPCHECK_YL) test_regress/t/*.c* test_regress/t/*.h
@ -470,6 +472,17 @@ clang-format:
|| echo "*** You are not using clang-format 10.0, indents may differ from master's ***" || echo "*** You are not using clang-format 10.0, indents may differ from master's ***"
$(CLANGFORMAT) $(CLANGFORMAT_FLAGS) $(CLANGFORMAT_FILES) $(CLANGFORMAT) $(CLANGFORMAT_FLAGS) $(CLANGFORMAT_FILES)
YAPF = yapf
YAPF_FLAGS = -i
YAPF_FILES = \
nodist/fuzzer/actual_fail \
nodist/fuzzer/generate_dictionary \
examples/xml_py/vl_file_copy \
examples/xml_py/vl_hier_graph \
yapf:
$(YAPF) $(YAPF_FLAGS) $(YAPF_FILES)
ftp: info ftp: info
install-msg: install-msg:

View File

@ -10,24 +10,29 @@ import sys
import tempfile import tempfile
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from shutil import copy2 from shutil import copy2
from pprint import pprint,pformat from pprint import pprint, pformat
####################################################################### #######################################################################
class VlFileCopy: class VlFileCopy:
def __init__(self, def __init__(
verilator_args, # presently all verilator options are passed-thru self,
# ideally this script would check against options mentioned in help verilator_args, # presently all verilator options are passed-thru
debug=0, # ideally this script would check against options mentioned in help
output_dir='copied'): # directory name we output file uses debug=0,
output_dir='copied'): # directory name we output file uses
self.debug = debug self.debug = debug
xml_temp = tempfile.NamedTemporaryFile() xml_temp = tempfile.NamedTemporaryFile()
args = ['--xml-output', xml_temp.name, args = [
'--bbox-sys', # Parse some stuff can't translate '--xml-output',
'--bbox-unsup', xml_temp.name,
'--prefix vlxml'] # So we know name of .xml output '--bbox-sys', # Parse some stuff can't translate
'--bbox-unsup',
'--prefix vlxml'
] # So we know name of .xml output
args += verilator_args args += verilator_args
self.run_verilator(args) self.run_verilator(args)
self.tree = ET.parse(xml_temp.name) self.tree = ET.parse(xml_temp.name)
@ -55,16 +60,18 @@ class VlFileCopy:
print("\t%s " % command) print("\t%s " % command)
status = subprocess.call(command, shell=True) status = subprocess.call(command, shell=True)
if status != 0: if status != 0:
raise Exception("Command failed running Verilator with '"+command+"', stopped") raise Exception("Command failed running Verilator with '" +
command + "', stopped")
####################################################################### #######################################################################
if __name__=='__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
allow_abbrev=False, allow_abbrev=False,
formatter_class=argparse.RawTextHelpFormatter, formatter_class=argparse.RawTextHelpFormatter,
description= description=
"""Example of using Verilator XML output to copy a list of files to an """Example of using Verilator XML output to copy a list of files to an
output directory (-odir, defaults to 'copied'), e.g. to easily create a output directory (-odir, defaults to 'copied'), e.g. to easily create a
tarball of the design to pass to others. tarball of the design to pass to others.
@ -72,8 +79,7 @@ Example usage:
vl_file_copy -f input.vc top.v -odir mycopy vl_file_copy -f input.vc top.v -odir mycopy
# This will make at least mycopy/top.v # This will make at least mycopy/top.v
""", """,
epilog= epilog="""All other arguments are pass-thru to Verilator: e.g.:
"""All other arguments are pass-thru to Verilator: e.g.:
+define+<var>=<value> Set preprocessor define +define+<var>=<value> Set preprocessor define
-F <file> Parse options from a file, relatively -F <file> Parse options from a file, relatively
@ -87,26 +93,30 @@ Example usage:
This file ONLY is placed under the Creative Commons Public Domain, for This file ONLY is placed under the Creative Commons Public Domain, for
any use, without warranty, 2019 by Wilson Snyder. any use, without warranty, 2019 by Wilson Snyder.
SPDX-License-Identifier: CC0-1.0 SPDX-License-Identifier: CC0-1.0
""" """)
) parser.add_argument('-debug',
parser.add_argument('-debug', '--debug', '--debug',
action='store_const', const=9, action='store_const',
const=9,
help='enable debug') help='enable debug')
parser.add_argument('-odir', '--odir', parser.add_argument('-odir',
action='store', metavar='directory', required=True, '--odir',
action='store',
metavar='directory',
required=True,
help='target output directory') help='target output directory')
(args, rem) = parser.parse_known_args() (args, rem) = parser.parse_known_args()
print("NOTE: vl_file_copy is only an example starting point for writing your own tool.") print(
"NOTE: vl_file_copy is only an example starting point for writing your own tool."
)
# That is: # That is:
# 1. We will accept basic patches # 1. We will accept basic patches
# 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir) # 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir)
# 3. "make install" will not install this. # 3. "make install" will not install this.
# 4. This has not had production-worthy validation. # 4. This has not had production-worthy validation.
fc = VlFileCopy(output_dir = args.odir, fc = VlFileCopy(output_dir=args.odir, debug=args.debug, verilator_args=rem)
debug = args.debug,
verilator_args = rem)
###################################################################### ######################################################################
### Local Variables: ### Local Variables:

View File

@ -10,26 +10,31 @@ import sys
import tempfile import tempfile
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from shutil import copy2 from shutil import copy2
from pprint import pprint,pformat from pprint import pprint, pformat
####################################################################### #######################################################################
class VlHierGraph: class VlHierGraph:
def __init__(self, def __init__(
verilator_args, # presently all verilator options are passed-thru self,
# ideally this script would check against options mentioned in help verilator_args, # presently all verilator options are passed-thru
debug=0, # ideally this script would check against options mentioned in help
output_filename='graph.dot'): # output filename debug=0,
output_filename='graph.dot'): # output filename
self.debug = debug self.debug = debug
self.next_vertex_number = 0 self.next_vertex_number = 0
self.name_to_number = {} self.name_to_number = {}
xml_temp = tempfile.NamedTemporaryFile() xml_temp = tempfile.NamedTemporaryFile()
args = ['--xml-output', xml_temp.name, args = [
'--bbox-sys', # Parse some stuff can't translate '--xml-output',
'--bbox-unsup', xml_temp.name,
'--prefix vlxml'] # So we know name of .xml output '--bbox-sys', # Parse some stuff can't translate
'--bbox-unsup',
'--prefix vlxml'
] # So we know name of .xml output
args += verilator_args args += verilator_args
self.run_verilator(args) self.run_verilator(args)
self.tree = ET.parse(xml_temp.name) self.tree = ET.parse(xml_temp.name)
@ -39,7 +44,9 @@ class VlHierGraph:
fh.write("digraph {\n") fh.write("digraph {\n")
fh.write(" dpi=300;\n") fh.write(" dpi=300;\n")
fh.write(" order=LR;\n") fh.write(" order=LR;\n")
fh.write(" node [fontsize=8 shape=\"box\" margin=0.01 width=0 height=0]") fh.write(
" node [fontsize=8 shape=\"box\" margin=0.01 width=0 height=0]"
)
fh.write(" edge [fontsize=6]") fh.write(" edge [fontsize=6]")
# Find cells # Find cells
root = self.tree.getroot() root = self.tree.getroot()
@ -48,8 +55,7 @@ class VlHierGraph:
# origNames are before parameterization, name if after # origNames are before parameterization, name if after
mod_name = module.get('name') mod_name = module.get('name')
mod_number = self.name_to_vertex_number(mod_name) mod_number = self.name_to_vertex_number(mod_name)
fh.write(" n%d [label=\"%s\"" fh.write(" n%d [label=\"%s\"" % (mod_number, mod_name))
% (mod_number, mod_name))
if module.get('topModule'): if module.get('topModule'):
fh.write(" color=\"red\" rank=1") fh.write(" color=\"red\" rank=1")
fh.write("];\n") fh.write("];\n")
@ -58,8 +64,8 @@ class VlHierGraph:
inst_name = instance.get('name') inst_name = instance.get('name')
def_name = instance.get('defName') def_name = instance.get('defName')
def_number = self.name_to_vertex_number(def_name) def_number = self.name_to_vertex_number(def_name)
fh.write(" n%d->n%d [label=\"%s\"];\n" fh.write(" n%d->n%d [label=\"%s\"];\n" %
% (mod_number, def_number, inst_name)); (mod_number, def_number, inst_name))
fh.write("}\n") fh.write("}\n")
@ -80,24 +86,25 @@ class VlHierGraph:
print("\t%s " % command) print("\t%s " % command)
status = subprocess.call(command, shell=True) status = subprocess.call(command, shell=True)
if status != 0: if status != 0:
raise Exception("Command failed running Verilator with '"+command+"', stopped") raise Exception("Command failed running Verilator with '" +
command + "', stopped")
####################################################################### #######################################################################
if __name__=='__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
allow_abbrev=False, allow_abbrev=False,
formatter_class=argparse.RawTextHelpFormatter, formatter_class=argparse.RawTextHelpFormatter,
description= description=
"""Example of using Verilator XML output to create a .dot file showing the """Example of using Verilator XML output to create a .dot file showing the
design module hierarchy. design module hierarchy.
Example usage: Example usage:
vl_hier_graph -f input.vc top.v -o graph.dot vl_hier_graph -f input.vc top.v -o graph.dot
dot -Tpdf -o graph.pdf graph.dot dot -Tpdf -o graph.pdf graph.dot
""", """,
epilog= epilog="""All other arguments are pass-thru to Verilator: e.g.:
"""All other arguments are pass-thru to Verilator: e.g.:
+define+<var>=<value> Set preprocessor define +define+<var>=<value> Set preprocessor define
-F <file> Parse options from a file, relatively -F <file> Parse options from a file, relatively
@ -111,26 +118,32 @@ Example usage:
This file ONLY is placed under the Creative Commons Public Domain, for This file ONLY is placed under the Creative Commons Public Domain, for
any use, without warranty, 2019 by Wilson Snyder. any use, without warranty, 2019 by Wilson Snyder.
SPDX-License-Identifier: CC0-1.0 SPDX-License-Identifier: CC0-1.0
""" """)
) parser.add_argument('-debug',
parser.add_argument('-debug', '--debug', '--debug',
action='store_const', const=9, action='store_const',
const=9,
help='enable debug') help='enable debug')
parser.add_argument('-o', '--o', parser.add_argument('-o',
action='store', metavar='filename', required=True, '--o',
action='store',
metavar='filename',
required=True,
help='output filename') help='output filename')
(args, rem) = parser.parse_known_args() (args, rem) = parser.parse_known_args()
print("NOTE: vl_hier_graph is only an example starting point for writing your own tool.") print(
"NOTE: vl_hier_graph is only an example starting point for writing your own tool."
)
# That is: # That is:
# 1. We will accept basic patches # 1. We will accept basic patches
# 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir) # 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir)
# 3. "make install" will not install this. # 3. "make install" will not install this.
# 4. This has not had production-worthy validation. # 4. This has not had production-worthy validation.
fc = VlHierGraph(output_filename = args.o, fc = VlHierGraph(output_filename=args.o,
debug = args.debug, debug=args.debug,
verilator_args = rem) verilator_args=rem)
###################################################################### ######################################################################
### Local Variables: ### Local Variables:

View File

@ -16,6 +16,7 @@ from glob import glob
from subprocess import getstatusoutput from subprocess import getstatusoutput
from argparse import ArgumentParser from argparse import ArgumentParser
def interesting(s): def interesting(s):
if 'assert' in s: return 1 if 'assert' in s: return 1
if 'Assert' in s: return 1 if 'Assert' in s: return 1
@ -33,17 +34,19 @@ def interesting(s):
def main(): def main():
p = ArgumentParser() p = ArgumentParser()
p.add_argument('--dir',default='out1/queue') p.add_argument('--dir', default='out1/queue')
args = p.parse_args() args = p.parse_args()
for infile in glob(args.dir+'/*'): for infile in glob(args.dir + '/*'):
# Input filenames are known not to contain spaces or other unusual # Input filenames are known not to contain spaces or other unusual
# characters, therefore this works. # characters, therefore this works.
status,output = getstatusoutput('../../bin/verilator_bin --cc '+infile) status, output = getstatusoutput('../../bin/verilator_bin --cc ' +
infile)
if interesting(output): if interesting(output):
print(infile) print(infile)
print(status) print(status)
print(output) print(output)
if __name__=='__main__':
if __name__ == '__main__':
main() main()

View File

@ -15,7 +15,8 @@
from subprocess import getstatusoutput from subprocess import getstatusoutput
from os import system from os import system
def take_while(f,a):
def take_while(f, a):
# any(a) => (a->bool)->[a]->[a] # any(a) => (a->bool)->[a]->[a]
# Does the same think as Haskell's takewhile. # Does the same think as Haskell's takewhile.
out = [] out = []
@ -26,44 +27,50 @@ def take_while(f,a):
return out return out
return out return out
def skip_while(f,a):
def skip_while(f, a):
# any(a) => (a->bool)->[a]->[a] # any(a) => (a->bool)->[a]->[a]
# Basically, the opposite thing from skipwhile # Basically, the opposite thing from skipwhile
while len(a) and f(a[0]): while len(a) and f(a[0]):
a = a[1:] a = a[1:]
return a return a
def print_lines(a): def print_lines(a):
# printable(a) => [a]->void # printable(a) => [a]->void
for elem in a: for elem in a:
print(elem) print(elem)
def write_file(filename,contents):
def write_file(filename, contents):
# str->str->void # str->str->void
f = open(filename,'w') f = open(filename, 'w')
f.write(contents) f.write(contents)
def parse_line(s): def parse_line(s):
# str->maybe str # str->maybe str
if len(s)==0: return if len(s) == 0: return
part = skip_while(lambda x: x!='"',s) part = skip_while(lambda x: x != '"', s)
if len(part)==0 or part[0]!='"': return None if len(part) == 0 or part[0] != '"': return None
literal_part = take_while(lambda x: x!='"',part[1:]) literal_part = take_while(lambda x: x != '"', part[1:])
return ''.join(filter(lambda x: x!='\\',literal_part)) return ''.join(filter(lambda x: x != '\\', literal_part))
def main(): def main():
status,output = getstatusoutput('flex -T ../../src/verilog.l') status, output = getstatusoutput('flex -T ../../src/verilog.l')
assert status==0 assert status == 0
lines = output.splitlines() lines = output.splitlines()
lines = take_while(lambda x: 'beginning dump of nfa' not in x,lines) lines = take_while(lambda x: 'beginning dump of nfa' not in x, lines)
tokens = set(filter(lambda x: x,map(parse_line,lines))) tokens = set(filter(lambda x: x, map(parse_line, lines)))
dirname = 'dictionary' dirname = 'dictionary'
r = system('mkdir -p '+dirname) r = system('mkdir -p ' + dirname)
assert(r==0) assert (r == 0)
for i,token in enumerate(tokens): for i, token in enumerate(tokens):
write_file(dirname+'/'+str(i),token) write_file(dirname + '/' + str(i), token)
if __name__=='__main__':
if __name__ == '__main__':
main() main()

View File

@ -82,6 +82,8 @@ config_rev.h: ${srcdir}/config_rev.pl $(GIT_CHANGE_DEP)
$(PERL) ${srcdir}/config_rev.pl ${srcdir} >$@ $(PERL) ${srcdir}/config_rev.pl ${srcdir} >$@
# Human convenience # Human convenience
format:
$(MAKE) -C .. $@
clang-format: clang-format:
$(MAKE) -C .. $@ $(MAKE) -C .. $@