Index: src/trusted/validator/x86/testing/tf/hex2tf.py |
diff --git a/src/trusted/validator/x86/testing/tf/hex2tf.py b/src/trusted/validator/x86/testing/tf/hex2tf.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..e0e4245d9889b562740c40c8cf77ed4cdfab5c6e |
--- /dev/null |
+++ b/src/trusted/validator/x86/testing/tf/hex2tf.py |
@@ -0,0 +1,112 @@ |
+#!/usr/bin/python |
+# |
+# Copyright (c) 2012 The Native Client Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import glob |
+import optparse |
+import os |
+import sys |
+ |
+import annotate_tf |
+import asm |
+import utils |
+import val_runner |
+ |
+ |
+def Convert(bits, hex_filename, tf_filename): |
+ with open(hex_filename) as hex: |
+ with open(tf_filename, 'w') as fout: |
+ fout.write('BITS: %d\n' % bits) |
+ fout.write('\n') |
+ for line in hex: |
+ line = line.strip() |
+ if line == '' or line.startswith('#'): |
+ fout.write('%s\n' % line) |
+ continue |
+ |
+ data = utils.ReadableHexToData(line) |
+ a = asm.DisassembleReversibly(bits, data) |
+ if a is not None: |
+ fout.write('asm: %s\n' % a) |
+ |
+ fout.write('hex: %s\n' % line) |
+ fout.write('\n') |
+ |
+ |
+def main(): |
+ parser = optparse.OptionParser( |
+ '%prog [options] <hex_file or wildcard> [<output dir>]' |
+ ) |
+ parser.add_option('--bits', dest='bits', type=int) |
+ |
+ options, args = parser.parse_args() |
+ |
+ if options.bits not in [32, 64]: |
+ parser.error('specify --bits 32 or --bits 64') |
+ |
+ if len(args) not in [1, 2]: |
+ parser.error('specify input hex file and output tf file') |
+ |
+ if len(args) == 2: |
+ pattern, output_dir = args |
+ if not os.path.exists(output_dir): |
+ os.makedirs(output_dir) |
+ else: |
+ (pattern,) = args |
+ output_dir = None |
+ |
+ for hex_path in glob.glob(pattern): |
+ hex_dir, hex_file = os.path.split(hex_path) |
+ hex_base, hex_ext = os.path.splitext(hex_file) |
+ if hex_ext.lower() != '.hex': |
+ print 'Supposed hex file %s does not have .hex extension' % hex_path |
+ sys.exit(1) |
+ tf_file = hex_base+'.tf' |
Mark Seaborn
2012/09/10 19:38:06
Nit: put spaces around '+'
Vlad Shcherbina
2012/09/11 14:26:00
Done.
|
+ |
+ if output_dir is not None: |
+ tf_path = os.path.join(output_dir, tf_file) |
+ else: |
+ tf_path = os.path.join(hex_dir, tf_file) |
+ |
+ print hex_path, tf_path |
+ |
+ Convert(options.bits, hex_path, tf_path) |
+ |
+ with open(tf_path) as fin: |
+ original_lines = fin.readlines() |
+ |
+ # We annotate freshly constructed tf, but instead of actually running |
+ # ncval on it, we take results from corresponding hex file. All parsing |
+ # is done as usual, we only substitute |
+ |
Vlad Shcherbina
2012/09/10 16:56:08
In anticipation of possible questions: there are t
|
+ def MonkeyRunner(hex_name): |
+ # hex_name passed here is the name of temporary hex file, |
+ # so to obtain path to gold we use hex_path instead. |
+ golden_ext = {32: '.nval', 64: '.rval'}[options.bits] |
+ golden_path = os.path.splitext(hex_path)[0]+golden_ext |
+ with open(golden_path) as fin: |
+ return fin.readlines() |
+ |
+ if options.bits == 32: |
+ backup_runner = val_runner.RunNcVal32 |
+ val_runner.RunNcVal32 = MonkeyRunner |
+ elif options.bits == 64: |
+ backup_runner = val_runner.RunNcVal64 |
+ val_runner.RunNcVal64 = MonkeyRunner |
+ |
+ try: |
+ annotated_lines = annotate_tf.Annotate(['nc'], original_lines) |
+ finally: |
+ if options.bits == 32: |
+ val_runner.RunNcVal32 = backup_runner |
+ elif options.bits == 64: |
+ val_runner.RunNcVal64 = backup_runner |
+ |
+ with open(tf_path, 'w') as fout: |
+ fout.writelines(annotated_lines) |
+ |
+ |
+if __name__ == '__main__': |
+ main() |