16657Snate@binkert.org# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
26657Snate@binkert.org# Copyright (c) 2009 The Hewlett-Packard Development Company
36657Snate@binkert.org# All rights reserved.
46657Snate@binkert.org#
56657Snate@binkert.org# Redistribution and use in source and binary forms, with or without
66657Snate@binkert.org# modification, are permitted provided that the following conditions are
76657Snate@binkert.org# met: redistributions of source code must retain the above copyright
86657Snate@binkert.org# notice, this list of conditions and the following disclaimer;
96657Snate@binkert.org# redistributions in binary form must reproduce the above copyright
106657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
116657Snate@binkert.org# documentation and/or other materials provided with the distribution;
126657Snate@binkert.org# neither the name of the copyright holders nor the names of its
136657Snate@binkert.org# contributors may be used to endorse or promote products derived from
146657Snate@binkert.org# this software without specific prior written permission.
156657Snate@binkert.org#
166657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276657Snate@binkert.org
2812563Sgabeblack@google.comfrom __future__ import print_function
2912563Sgabeblack@google.com
306657Snate@binkert.orgimport os
316657Snate@binkert.orgimport sys
326657Snate@binkert.org
336657Snate@binkert.orgfrom slicc.parser import SLICC
346657Snate@binkert.org
3512765Sjason@lowepower.comusage="%prog [options] <slicc file> ... "
366657Snate@binkert.orgversion="%prog v0.4"
376657Snate@binkert.orgbrief_copyright='''
386657Snate@binkert.orgCopyright (c) 1999-2008 Mark D. Hill and David A. Wood
396657Snate@binkert.orgCopyright (c) 2009 The Hewlett-Packard Development Company
406657Snate@binkert.orgAll Rights Reserved.
416657Snate@binkert.org'''
4212765Sjason@lowepower.comhelp_details = '''This is intended to be used to process slicc files as a
4312765Sjason@lowepower.comstandalone script. This script assumes that it is running in a directory under
4412765Sjason@lowepower.comgem5/ (e.g., gem5/temp). It takes a single argument: The path to a *.slicc
4512765Sjason@lowepower.comfile. By default it generates the C++ code in the directory generated/. This
4612765Sjason@lowepower.comscript can also generate the html SLICC output. See src/mem/slicc/main.py for
4712765Sjason@lowepower.commore details.'''
486657Snate@binkert.org
496657Snate@binkert.orgdef nprint(format, *args):
506657Snate@binkert.org    pass
516657Snate@binkert.org
526657Snate@binkert.orgdef eprint(format, *args):
536657Snate@binkert.org    if args:
546657Snate@binkert.org        format = format % args
556657Snate@binkert.org
5612563Sgabeblack@google.com    print(format, file=sys.stderr)
576657Snate@binkert.org
586657Snate@binkert.orgdef main(args=None):
596657Snate@binkert.org    import optparse
606657Snate@binkert.org
616657Snate@binkert.org    parser = optparse.OptionParser(usage=usage, version=version,
6212765Sjason@lowepower.com                                   epilog=help_details,
636657Snate@binkert.org                                   description=brief_copyright)
646657Snate@binkert.org    parser.add_option("-d", "--debug", default=False, action="store_true",
656657Snate@binkert.org                      help="Turn on PLY debugging")
666657Snate@binkert.org    parser.add_option("-C", "--code-path", default="generated",
676657Snate@binkert.org                      help="Path where C++ code output code goes")
686657Snate@binkert.org    parser.add_option("-H", "--html-path",
696657Snate@binkert.org                      help="Path where html output goes")
7012765Sjason@lowepower.com    parser.add_option("-F", "--print-files", action='store_true',
716657Snate@binkert.org                      help="Print files that SLICC will generate")
728453Snate@binkert.org    parser.add_option("--tb", "--traceback", action='store_true',
738453Snate@binkert.org                      help="print traceback on error")
746657Snate@binkert.org    parser.add_option("-q", "--quiet",
756657Snate@binkert.org                      help="don't print messages")
766657Snate@binkert.org    opts,files = parser.parse_args(args=args)
776657Snate@binkert.org
788454Snate@binkert.org    if len(files) != 1:
796657Snate@binkert.org        parser.print_help()
806657Snate@binkert.org        sys.exit(2)
816657Snate@binkert.org
8212765Sjason@lowepower.com    slicc_file = files[0]
8312765Sjason@lowepower.com    if not slicc_file.endswith('.slicc'):
8412765Sjason@lowepower.com        print("Must specify a .slicc file with a list of state machine files")
8512765Sjason@lowepower.com        parser.print_help()
8612765Sjason@lowepower.com        sys.exit(2)
8712765Sjason@lowepower.com
886657Snate@binkert.org    output = nprint if opts.quiet else eprint
896657Snate@binkert.org
906657Snate@binkert.org    output("SLICC v0.4")
918453Snate@binkert.org    output("Parsing...")
928453Snate@binkert.org
9314184Sgabeblack@google.com    protocol_base = os.path.join(os.path.dirname(__file__),
9414184Sgabeblack@google.com                                 '..', 'ruby', 'protocol')
9512765Sjason@lowepower.com    slicc = SLICC(slicc_file, protocol_base, verbose=True, debug=opts.debug,
9612765Sjason@lowepower.com                  traceback=opts.tb)
9712765Sjason@lowepower.com
986657Snate@binkert.org
996657Snate@binkert.org    if opts.print_files:
1006714Ssteve.reinhardt@amd.com        for i in sorted(slicc.files()):
10112563Sgabeblack@google.com            print('    %s' % i)
1026657Snate@binkert.org    else:
1038453Snate@binkert.org        output("Processing AST...")
1048453Snate@binkert.org        slicc.process()
1056657Snate@binkert.org
1066657Snate@binkert.org        if opts.html_path:
1078453Snate@binkert.org            output("Writing HTML files...")
1086657Snate@binkert.org            slicc.writeHTMLFiles(opts.html_path)
1096657Snate@binkert.org
11012765Sjason@lowepower.com        output("Writing C++ files...")
11112765Sjason@lowepower.com        slicc.writeCodeFiles(opts.code_path, [])
11212765Sjason@lowepower.com
11312765Sjason@lowepower.com
1148453Snate@binkert.org    output("SLICC is Done.")
1156657Snate@binkert.org
1166657Snate@binkert.orgif __name__ == "__main__":
1176657Snate@binkert.org    main()
118