OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 3 # Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 import glob |
| 8 import optparse |
| 9 import os |
| 10 import sys |
| 11 |
| 12 import annotate_tf |
| 13 import asm |
| 14 import utils |
| 15 import val_runner |
| 16 |
| 17 |
| 18 def Convert(bits, hex_filename, tf_filename): |
| 19 with open(hex_filename) as hex: |
| 20 with open(tf_filename, 'w') as fout: |
| 21 fout.write('BITS: %d\n' % bits) |
| 22 fout.write('\n') |
| 23 for line in hex: |
| 24 line = line.strip() |
| 25 if line == '' or line.startswith('#'): |
| 26 fout.write('%s\n' % line) |
| 27 continue |
| 28 |
| 29 data = utils.ReadableHexToData(line) |
| 30 a = asm.DisassembleReversibly(bits, data) |
| 31 if a is not None: |
| 32 fout.write('asm: %s\n' % a) |
| 33 |
| 34 fout.write('hex: %s\n' % line) |
| 35 fout.write('\n') |
| 36 |
| 37 |
| 38 def main(): |
| 39 parser = optparse.OptionParser( |
| 40 '%prog [options] <hex_file or wildcard> [<output dir>]' |
| 41 ) |
| 42 parser.add_option('--bits', dest='bits', type=int) |
| 43 |
| 44 options, args = parser.parse_args() |
| 45 |
| 46 if options.bits not in [32, 64]: |
| 47 parser.error('specify --bits 32 or --bits 64') |
| 48 |
| 49 if len(args) not in [1, 2]: |
| 50 parser.error('specify input hex file and output tf file') |
| 51 |
| 52 if len(args) == 2: |
| 53 pattern, output_dir = args |
| 54 if not os.path.exists(output_dir): |
| 55 os.makedirs(output_dir) |
| 56 else: |
| 57 (pattern,) = args |
| 58 output_dir = None |
| 59 |
| 60 for hex_path in glob.glob(pattern): |
| 61 hex_dir, hex_file = os.path.split(hex_path) |
| 62 hex_base, hex_ext = os.path.splitext(hex_file) |
| 63 if hex_ext.lower() != '.hex': |
| 64 print 'Supposed hex file %s does not have .hex extension' % hex_path |
| 65 sys.exit(1) |
| 66 tf_file = hex_base + '.tf' |
| 67 |
| 68 if output_dir is not None: |
| 69 tf_path = os.path.join(output_dir, tf_file) |
| 70 else: |
| 71 tf_path = os.path.join(hex_dir, tf_file) |
| 72 |
| 73 print hex_path, tf_path |
| 74 |
| 75 Convert(options.bits, hex_path, tf_path) |
| 76 |
| 77 with open(tf_path) as fin: |
| 78 original_lines = fin.readlines() |
| 79 |
| 80 # We annotate freshly constructed tf, but instead of actually running |
| 81 # ncval on it, we take results from corresponding hex file. All parsing |
| 82 # is done as usual, we only substitute |
| 83 |
| 84 def MonkeyRunner(hex_name): |
| 85 # hex_name passed here is the name of temporary hex file, |
| 86 # so to obtain path to gold we use hex_path instead. |
| 87 golden_ext = {32: '.nval', 64: '.rval'}[options.bits] |
| 88 golden_path = os.path.splitext(hex_path)[0]+golden_ext |
| 89 with open(golden_path) as fin: |
| 90 return fin.readlines() |
| 91 |
| 92 if options.bits == 32: |
| 93 backup_runner = val_runner.RunNcVal32 |
| 94 val_runner.RunNcVal32 = MonkeyRunner |
| 95 elif options.bits == 64: |
| 96 backup_runner = val_runner.RunNcVal64 |
| 97 val_runner.RunNcVal64 = MonkeyRunner |
| 98 |
| 99 try: |
| 100 annotated_lines = annotate_tf.Annotate(['nc'], original_lines) |
| 101 finally: |
| 102 if options.bits == 32: |
| 103 val_runner.RunNcVal32 = backup_runner |
| 104 elif options.bits == 64: |
| 105 val_runner.RunNcVal64 = backup_runner |
| 106 |
| 107 with open(tf_path, 'w') as fout: |
| 108 fout.writelines(annotated_lines) |
| 109 |
| 110 |
| 111 if __name__ == '__main__': |
| 112 main() |
OLD | NEW |