1b8021494Sopenharmony_ci#!/usr/bin/env python3
2b8021494Sopenharmony_ci
3b8021494Sopenharmony_ci# Copyright 2015, VIXL authors
4b8021494Sopenharmony_ci# All rights reserved.
5b8021494Sopenharmony_ci#
6b8021494Sopenharmony_ci# Redistribution and use in source and binary forms, with or without
7b8021494Sopenharmony_ci# modification, are permitted provided that the following conditions are met:
8b8021494Sopenharmony_ci#
9b8021494Sopenharmony_ci#   * Redistributions of source code must retain the above copyright notice,
10b8021494Sopenharmony_ci#     this list of conditions and the following disclaimer.
11b8021494Sopenharmony_ci#   * Redistributions in binary form must reproduce the above copyright notice,
12b8021494Sopenharmony_ci#     this list of conditions and the following disclaimer in the documentation
13b8021494Sopenharmony_ci#     and/or other materials provided with the distribution.
14b8021494Sopenharmony_ci#   * Neither the name of ARM Limited nor the names of its contributors may be
15b8021494Sopenharmony_ci#     used to endorse or promote products derived from this software without
16b8021494Sopenharmony_ci#     specific prior written permission.
17b8021494Sopenharmony_ci#
18b8021494Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
19b8021494Sopenharmony_ci# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20b8021494Sopenharmony_ci# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21b8021494Sopenharmony_ci# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22b8021494Sopenharmony_ci# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b8021494Sopenharmony_ci# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24b8021494Sopenharmony_ci# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25b8021494Sopenharmony_ci# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26b8021494Sopenharmony_ci# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27b8021494Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b8021494Sopenharmony_ci
29b8021494Sopenharmony_ciimport os
30b8021494Sopenharmony_ciimport sys
31b8021494Sopenharmony_ciimport argparse
32b8021494Sopenharmony_ciimport re
33b8021494Sopenharmony_ciimport util
34b8021494Sopenharmony_ci
35b8021494Sopenharmony_cicopyright_header = """// Copyright 2015, VIXL authors
36b8021494Sopenharmony_ci// All rights reserved.
37b8021494Sopenharmony_ci//
38b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
39b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
40b8021494Sopenharmony_ci//
41b8021494Sopenharmony_ci//   * Redistributions of source code must retain the above copyright notice,
42b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer.
43b8021494Sopenharmony_ci//   * Redistributions in binary form must reproduce the above copyright notice,
44b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer in the documentation
45b8021494Sopenharmony_ci//     and/or other materials provided with the distribution.
46b8021494Sopenharmony_ci//   * Neither the name of ARM Limited nor the names of its contributors may be
47b8021494Sopenharmony_ci//     used to endorse or promote products derived from this software without
48b8021494Sopenharmony_ci//     specific prior written permission.
49b8021494Sopenharmony_ci//
50b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
51b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
54b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60b8021494Sopenharmony_ci
61b8021494Sopenharmony_ci"""
62b8021494Sopenharmony_ci
63b8021494Sopenharmony_cimaster_trace_header = """
64b8021494Sopenharmony_ci// This file holds the expected results for the instructions tested by
65b8021494Sopenharmony_ci// test-simulator-aarch64.
66b8021494Sopenharmony_ci//
67b8021494Sopenharmony_ci// If you update input lists in test-simulator-inputs-aarch64.h, or add a new
68b8021494Sopenharmony_ci// test to test-simulator-aarch64.cc, please run
69b8021494Sopenharmony_ci// tools/generate_simulator_traces.py on a reference platform to regenerate
70b8021494Sopenharmony_ci// this file and trace files.
71b8021494Sopenharmony_ci//
72b8021494Sopenharmony_ci
73b8021494Sopenharmony_ci#ifndef VIXL_TEST_AARCH64_SIMULATOR_TRACES_AARCH64_H_
74b8021494Sopenharmony_ci#define VIXL_TEST_AARCH64_SIMULATOR_TRACES_AARCH64_H_
75b8021494Sopenharmony_ci
76b8021494Sopenharmony_ciextern "C" {
77b8021494Sopenharmony_ci#include <stdint.h>
78b8021494Sopenharmony_ci}
79b8021494Sopenharmony_ci
80b8021494Sopenharmony_ci// To add a new simulator test to test-simulator-aarch64.cc, add placeholder array(s)
81b8021494Sopenharmony_ci// below to build test-simulator-aarch64 for reference platform. Then, run
82b8021494Sopenharmony_ci// tools/generate_simulator_traces.py on a reference platform to regenerate this
83b8021494Sopenharmony_ci// file and traces files.
84b8021494Sopenharmony_ci
85b8021494Sopenharmony_ci// ---------------------------------------------------------------------
86b8021494Sopenharmony_ci// ADD DUMMY ARRAYS FOR NEW SIMULATOR TEST HERE.
87b8021494Sopenharmony_ci// ---------------------------------------------------------------------
88b8021494Sopenharmony_ciconst uint64_t kExpected_placeholder_64[] = {0};
89b8021494Sopenharmony_ciconst size_t kExpectedCount_placeholder_64 = 0;
90b8021494Sopenharmony_ci
91b8021494Sopenharmony_ciconst uint32_t kExpected_placeholder_32[] = {0};
92b8021494Sopenharmony_ciconst size_t kExpectedCount_placeholder_32 = 0;
93b8021494Sopenharmony_ci
94b8021494Sopenharmony_ci// ---------------------------------------------------------------------
95b8021494Sopenharmony_ci// Simulator test trace output files.
96b8021494Sopenharmony_ci// ---------------------------------------------------------------------
97b8021494Sopenharmony_ci"""
98b8021494Sopenharmony_cimaster_trace_footer = """
99b8021494Sopenharmony_ci#endif  // VIXL_TEST_AARCH64_SIMULATOR_TRACES_AARCH64_H_
100b8021494Sopenharmony_ci"""
101b8021494Sopenharmony_ci
102b8021494Sopenharmony_citrace_header = """
103b8021494Sopenharmony_ci// ---------------------------------------------------------------------
104b8021494Sopenharmony_ci// This file is auto generated using tools/generate_simulator_traces.py.
105b8021494Sopenharmony_ci//
106b8021494Sopenharmony_ci// PLEASE DO NOT EDIT.
107b8021494Sopenharmony_ci// ---------------------------------------------------------------------
108b8021494Sopenharmony_ci"""
109b8021494Sopenharmony_ci
110b8021494Sopenharmony_cidef BuildOptions(root):
111b8021494Sopenharmony_ci  result = argparse.ArgumentParser(description = 'Simulator test generator.')
112b8021494Sopenharmony_ci  result.add_argument('--runner', action='store',
113b8021494Sopenharmony_ci                      default=os.path.join(root, 'obj/latest/test/test-runner'),
114b8021494Sopenharmony_ci                      help='The test executable to run.')
115b8021494Sopenharmony_ci  result.add_argument('--aarch32-only', action='store_true')
116b8021494Sopenharmony_ci  result.add_argument('--aarch64-only', action='store_true')
117b8021494Sopenharmony_ci  result.add_argument('--out', action='store',
118b8021494Sopenharmony_ci                      default='test/aarch64/test-simulator-traces-aarch64.h')
119b8021494Sopenharmony_ci  result.add_argument('--filter', action='store', help='Test regexp filter.')
120b8021494Sopenharmony_ci  return result.parse_args()
121b8021494Sopenharmony_ci
122b8021494Sopenharmony_cidef ShouldGenerateAArch32(args):
123b8021494Sopenharmony_ci  return (not args.aarch32_only and not args.aarch64_only) or args.aarch32_only
124b8021494Sopenharmony_ci
125b8021494Sopenharmony_cidef ShouldGenerateAArch64(args):
126b8021494Sopenharmony_ci  return (not args.aarch32_only and not args.aarch64_only) or args.aarch64_only
127b8021494Sopenharmony_ci
128b8021494Sopenharmony_cidef GetAArch32Filename(test):
129b8021494Sopenharmony_ci  return test.lower().replace('_', '-') + '.h'
130b8021494Sopenharmony_ci
131b8021494Sopenharmony_cidef GetAArch64Filename(test):
132b8021494Sopenharmony_ci  return test.lower().replace('_', '-') + '-trace-aarch64.h'
133b8021494Sopenharmony_ci
134b8021494Sopenharmony_ciif __name__ == '__main__':
135b8021494Sopenharmony_ci  # $ROOT/tools/generate_simulator_traces.py
136b8021494Sopenharmony_ci  root_dir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
137b8021494Sopenharmony_ci  os.chdir(root_dir)
138b8021494Sopenharmony_ci
139b8021494Sopenharmony_ci  args = BuildOptions(root_dir)
140b8021494Sopenharmony_ci
141b8021494Sopenharmony_ci  # List all tests.
142b8021494Sopenharmony_ci  status, test_list = util.getstatusoutput(args.runner + ' --list')
143b8021494Sopenharmony_ci  if status != 0: util.abort('Failed to list all tests')
144b8021494Sopenharmony_ci
145b8021494Sopenharmony_ci  if ShouldGenerateAArch64(args):
146b8021494Sopenharmony_ci    # Run each simulator test (AARCH64_SIM_*) with the --generate_test_trace
147b8021494Sopenharmony_ci    # option, and use the output to create the traces header (from --out). In
148b8021494Sopenharmony_ci    # addition, the test-simulator-traces-aarch64.h file, the master trace file,
149b8021494Sopenharmony_ci    # which includes all other trace files is generated.
150b8021494Sopenharmony_ci
151b8021494Sopenharmony_ci    # Create master trace file.
152b8021494Sopenharmony_ci    master_trace_f = open(args.out, 'w')
153b8021494Sopenharmony_ci    master_trace_f.write(copyright_header)
154b8021494Sopenharmony_ci    master_trace_f.write(master_trace_header)
155b8021494Sopenharmony_ci    master_trace_f.write('\n\n')
156b8021494Sopenharmony_ci
157b8021494Sopenharmony_ci    # Find the AArch64 simulator tests.
158b8021494Sopenharmony_ci    tests = sorted([t for t in test_list.split() if 'AARCH64_SIM_' in t],
159b8021494Sopenharmony_ci                   key=lambda t: GetAArch64Filename(t))
160b8021494Sopenharmony_ci
161b8021494Sopenharmony_ci    for test in tests:
162b8021494Sopenharmony_ci      # Strip out 'AARCH64_' to get the name of the test.
163b8021494Sopenharmony_ci      test_name = test[len('AARCH64_'):]
164b8021494Sopenharmony_ci      trace_filename = GetAArch64Filename(test_name)
165b8021494Sopenharmony_ci      if not args.filter or re.compile(args.filter).search(test):
166b8021494Sopenharmony_ci        # Run each test.
167b8021494Sopenharmony_ci        print('Generating trace for ' + test);
168b8021494Sopenharmony_ci        cmd = ' '.join([args.runner, '--generate_test_trace', test])
169b8021494Sopenharmony_ci        status, output = util.getstatusoutput(cmd)
170b8021494Sopenharmony_ci        if status != 0: util.abort('Failed to run ' + cmd + '.')
171b8021494Sopenharmony_ci
172b8021494Sopenharmony_ci        # Create a new trace header file.
173b8021494Sopenharmony_ci        trace_f =  open("test/aarch64/traces/" + trace_filename, 'w')
174b8021494Sopenharmony_ci        trace_f.write(copyright_header)
175b8021494Sopenharmony_ci        trace_f.write(trace_header)
176b8021494Sopenharmony_ci        trace_f.write('\n')
177b8021494Sopenharmony_ci        trace_f.write("#ifndef VIXL_" + test_name.upper() + "_TRACE_AARCH64_H_\n")
178b8021494Sopenharmony_ci        trace_f.write("#define VIXL_" + test_name.upper() + "_TRACE_AARCH64_H_\n")
179b8021494Sopenharmony_ci        trace_f.write('\n')
180b8021494Sopenharmony_ci        trace_f.write(output)
181b8021494Sopenharmony_ci        trace_f.write('\n')
182b8021494Sopenharmony_ci        trace_f.write('\n' + "#endif  // VIXL_"
183b8021494Sopenharmony_ci                      + test_name.upper() + "_TRACE_AARCH64_H_" + '\n')
184b8021494Sopenharmony_ci        trace_f.close()
185b8021494Sopenharmony_ci
186b8021494Sopenharmony_ci      # Update master trace file.
187b8021494Sopenharmony_ci      master_trace_f.write(
188b8021494Sopenharmony_ci          '#include \"aarch64/traces/' + trace_filename + '\"\n')
189b8021494Sopenharmony_ci
190b8021494Sopenharmony_ci    # Close master trace file.
191b8021494Sopenharmony_ci    master_trace_f.write(master_trace_footer)
192b8021494Sopenharmony_ci    master_trace_f.close()
193b8021494Sopenharmony_ci
194b8021494Sopenharmony_ci  if ShouldGenerateAArch32(args):
195b8021494Sopenharmony_ci    # Run each test (AARCH32_{SIMULATOR,ASSEMBLER}_*) with the
196b8021494Sopenharmony_ci    # --generate_test_trace option.
197b8021494Sopenharmony_ci
198b8021494Sopenharmony_ci    # Find the AArch32 tests.
199b8021494Sopenharmony_ci    tests = sorted(
200b8021494Sopenharmony_ci        filter(
201b8021494Sopenharmony_ci            lambda t: 'AARCH32_SIMULATOR_' in t or ('AARCH32_ASSEMBLER_' in t
202b8021494Sopenharmony_ci                and not 'AARCH32_ASSEMBLER_NEGATIVE_' in t),
203b8021494Sopenharmony_ci            test_list.split()),
204b8021494Sopenharmony_ci        key=lambda t: GetAArch32Filename(t))
205b8021494Sopenharmony_ci    if args.filter:
206b8021494Sopenharmony_ci      tests = filter(re.compile(args.filter).search, tests)
207b8021494Sopenharmony_ci
208b8021494Sopenharmony_ci    for test in tests:
209b8021494Sopenharmony_ci      # Run each test.
210b8021494Sopenharmony_ci      print('Generating trace for ' + test);
211b8021494Sopenharmony_ci      # Strip out 'AARCH32_' to get the name of the test.
212b8021494Sopenharmony_ci      test_name = test[len('AARCH32_'):]
213b8021494Sopenharmony_ci
214b8021494Sopenharmony_ci      # An "and" instruction will be called "and_" since we cannot clash with
215b8021494Sopenharmony_ci      # the C++ operator. Rename "and_" to "and" to keep sane filenames.
216b8021494Sopenharmony_ci      test_name = test_name.replace('and_', 'and')
217b8021494Sopenharmony_ci
218b8021494Sopenharmony_ci      cmd = ' '.join([args.runner, '--generate_test_trace', test])
219b8021494Sopenharmony_ci      status, output = util.getstatusoutput(cmd)
220b8021494Sopenharmony_ci      if status != 0: util.abort('Failed to run ' + cmd + '.')
221b8021494Sopenharmony_ci
222b8021494Sopenharmony_ci      # Create a new trace header file.
223b8021494Sopenharmony_ci      trace_filename = GetAArch32Filename(test_name)
224b8021494Sopenharmony_ci      trace_f =  open("test/aarch32/traces/" + trace_filename, 'w')
225b8021494Sopenharmony_ci      trace_f.write(copyright_header)
226b8021494Sopenharmony_ci      trace_f.write(trace_header)
227b8021494Sopenharmony_ci      trace_f.write('\n')
228b8021494Sopenharmony_ci      trace_f.write("#ifndef VIXL_" + test_name.upper() + "_H_\n")
229b8021494Sopenharmony_ci      trace_f.write("#define VIXL_" + test_name.upper() + "_H_\n")
230b8021494Sopenharmony_ci      trace_f.write('\n')
231b8021494Sopenharmony_ci      trace_f.write(output)
232b8021494Sopenharmony_ci      trace_f.write('\n')
233b8021494Sopenharmony_ci      trace_f.write(
234b8021494Sopenharmony_ci          '\n' + "#endif  // VIXL_" + test_name.upper() + "_H_" + '\n')
235b8021494Sopenharmony_ci      trace_f.close()
236b8021494Sopenharmony_ci
237b8021494Sopenharmony_ci  print('Trace generation COMPLETE')
238