Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright 2012 The Native Client Authors. All rights reserved. | 3 # Copyright 2012 The Native Client Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can | 4 # Use of this source code is governed by a BSD-style license that can |
| 5 # be found in the LICENSE file. | 5 # be found in the LICENSE file. |
| 6 # | 6 # |
| 7 | 7 |
| 8 """Decoder Generator script. | 8 """Decoder Generator script. |
| 9 | 9 |
| 10 Usage: generate-decoder.py <table-file> <output-cc-file> <decoder-name> | 10 Usage: generate-decoder.py <table-file> <output-cc-file> <decoder-name> |
| 11 | 11 |
| 12 Note: If the file ends with named.{h,cc}, it is assumed that one should | 12 Note: If the file ends with named.{h,cc}, it is assumed that one should |
| 13 build the corresponding source file for named classes, so that testing | 13 build the corresponding source file for named classes, so that testing |
| 14 is easier to perform. In either case, the .h file will declare a decoder | 14 is easier to perform. In either case, the .h file will declare a decoder |
| 15 state (with the given decoder name) to decode instructions, while the | 15 state (with the given decoder name) to decode instructions, while the |
| 16 .cc file will define the methods for the declared decoder state. | 16 .cc file will define the methods for the declared decoder state. |
| 17 """ | 17 """ |
| 18 | 18 |
| 19 import re | 19 import re |
| 20 import sys | 20 import sys |
| 21 import dgen_input | 21 import dgen_input |
| 22 import dgen_output | 22 import dgen_decoder_output |
| 23 import dgen_test_output | |
| 23 | 24 |
| 24 def _localize_filename(filename): | 25 def _localize_filename(filename): |
| 25 """ Strips off directories above 'native_client', returning | 26 """ Strips off directories above 'native_client', returning |
| 26 a location neutral name for the file | 27 a location neutral name for the file |
| 27 """ | 28 """ |
| 28 m = re.match(r'.*/(native_client/.*)', filename) | 29 m = re.match(r'.*/(native_client/.*)', filename) |
| 29 if m: | 30 if m: |
| 30 return m.group(1) | 31 return m.group(1) |
| 31 else: | 32 else: |
| 32 # Don't know localized | 33 # Don't know localized |
| 33 return filename | 34 return filename |
| 34 | 35 |
| 35 def main(argv): | 36 def main(argv): |
| 36 table_filename = argv[1] | 37 table_filename = argv[1] |
| 37 output_filename = argv[2] | 38 output_filename = argv[2] |
| 38 decoder_name = argv[3] | 39 decoder_name = argv[3] |
| 39 | 40 |
| 40 print "Decoder Generator reading ", table_filename | 41 print "Decoder Generator reading ", table_filename |
| 41 f = open(table_filename, 'r') | 42 f = open(table_filename, 'r') |
| 42 tables = dgen_input.parse_tables(f) | 43 decoder = dgen_input.parse_tables(f) |
| 43 f.close() | 44 f.close() |
| 44 | 45 |
| 45 print "Successful - got %d tables." % len(tables) | 46 print "Successful - got %d tables." % len(decoder.tables()) |
| 46 | 47 |
| 47 print "Generating %s..." % output_filename | 48 print "Generating %s..." % output_filename |
| 48 f = open(output_filename, 'w') | 49 f = open(output_filename, 'w') |
| 49 | 50 |
| 50 if output_filename.endswith('named.h'): | 51 if output_filename.endswith('tests.cc'): |
| 51 dgen_output.generate_decoder_h(tables, | 52 dgen_test_output.generate_tests_cc(decoder, |
| 53 decoder_name, | |
| 54 f) | |
| 55 elif output_filename.endswith('named_classes.h'): | |
| 56 dgen_test_output.generate_named_classes_h(decoder, | |
|
robertm
2012/04/17 17:12:19
put args on new line to avoid 80char limit
Karl
2012/04/17 19:28:31
Done.
| |
| 57 decoder_name, | |
| 58 _localize_filename(output_filena me), | |
| 59 f) | |
| 60 elif output_filename.endswith('named_decoder.h'): | |
| 61 dgen_test_output.generate_named_decoder_h(decoder, | |
| 62 decoder_name, | |
| 63 _localize_filename(output_filena me), | |
| 64 f) | |
| 65 elif output_filename.endswith('.h'): | |
| 66 dgen_decoder_output.generate_h(decoder, | |
| 52 decoder_name, | 67 decoder_name, |
| 53 _localize_filename(output_filename), | 68 _localize_filename(output_filename), |
| 54 True, | 69 f) |
| 55 dgen_output.COutput(f)) | |
| 56 elif output_filename.endswith('.h'): | |
| 57 dgen_output.generate_decoder_h(tables, | |
| 58 decoder_name, | |
| 59 _localize_filename(output_filename), | |
| 60 False, | |
| 61 dgen_output.COutput(f)) | |
| 62 elif output_filename.endswith('named.cc'): | 70 elif output_filename.endswith('named.cc'): |
| 63 dgen_output.generate_decoder_cc(tables, | 71 dgen_test_output.generate_named_cc(decoder, |
| 72 decoder_name, | |
| 73 _localize_filename(output_filename), | |
| 74 f) | |
| 75 elif output_filename.endswith('.cc'): | |
| 76 dgen_decoder_output.generate_cc(decoder, | |
| 64 decoder_name, | 77 decoder_name, |
| 65 _localize_filename(output_filename), | 78 _localize_filename(output_filename), |
| 66 True, | 79 f) |
| 67 dgen_output.COutput(f)) | |
| 68 elif output_filename.endswith('.cc'): | |
| 69 dgen_output.generate_decoder_cc(tables, | |
| 70 decoder_name, | |
| 71 _localize_filename(output_filename), | |
| 72 False, | |
| 73 dgen_output.COutput(f)) | |
| 74 else: | 80 else: |
| 75 print 'Error: output filename not of form "*.{h,cc}"' | 81 print 'Error: output filename not of form "*.{h,cc}"' |
| 76 f.close() | 82 f.close() |
| 77 print "Completed." | 83 print "Completed." |
| 78 | 84 |
| 79 return 0 | 85 return 0 |
| 80 | 86 |
| 81 if __name__ == '__main__': | 87 if __name__ == '__main__': |
| 82 sys.exit(main(sys.argv)) | 88 sys.exit(main(sys.argv)) |
| OLD | NEW |