aboutsummaryrefslogtreecommitdiff
path: root/tools/generate_simulator_traces.py
blob: 3e25b0d3601580a8138c7a8bc120789332190822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python2.7

# Copyright 2015, VIXL authors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#   * Neither the name of ARM Limited nor the names of its contributors may be
#     used to endorse or promote products derived from this software without
#     specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import sys
import argparse
import re
import util

copyright_header = """// Copyright 2015, VIXL authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of ARM Limited nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""

master_trace_header = """
// This file holds the expected results for the instructions tested by
// test-simulator-aarch64.
//
// If you update input lists in test-simulator-inputs-aarch64.h, or add a new
// test to test-simulator-aarch64.cc, please run
// tools/generate_simulator_traces.py on a reference platform to regenerate
// this file and trace files.
//

#ifndef VIXL_TEST_AARCH64_SIMULATOR_TRACES_AARCH64_H_
#define VIXL_TEST_AARCH64_SIMULATOR_TRACES_AARCH64_H_

extern "C" {
#include <stdint.h>
}

// To add a new simulator test to test-simulator-aarch64.cc, add placeholder array(s)
// below to build test-simulator-aarch64 for reference platform. Then, run
// tools/generate_simulator_traces.py on a reference platform to regenerate this
// file and traces files.

// ---------------------------------------------------------------------
// ADD DUMMY ARRAYS FOR NEW SIMULATOR TEST HERE.
// ---------------------------------------------------------------------
const uint64_t kExpected_placeholder_64[] = {0};
const size_t kExpectedCount_placeholder_64 = 0;

const uint32_t kExpected_placeholder_32[] = {0};
const size_t kExpectedCount_placeholder_32 = 0;

// ---------------------------------------------------------------------
// Simulator test trace output files.
// ---------------------------------------------------------------------
"""
master_trace_footer = """
#endif  // VIXL_TEST_AARCH64_SIMULATOR_TRACES_AARCH64_H_
"""

trace_header = """
// ---------------------------------------------------------------------
// This file is auto generated using tools/generate_simulator_traces.py.
//
// PLEASE DO NOT EDIT.
// ---------------------------------------------------------------------
"""

def BuildOptions(root):
  result = argparse.ArgumentParser(description = 'Simulator test generator.')
  result.add_argument('--runner', action='store',
                      default=os.path.join(root, 'obj/latest/test/test-runner'),
                      help='The test executable to run.')
  result.add_argument('--aarch32-only', action='store_true')
  result.add_argument('--aarch64-only', action='store_true')
  result.add_argument('--out', action='store',
                      default='test/aarch64/test-simulator-traces-aarch64.h')
  result.add_argument('--filter', action='store', help='Test regexp filter.')
  return result.parse_args()

def ShouldGenerateAArch32(args):
  return (not args.aarch32_only and not args.aarch64_only) or args.aarch32_only

def ShouldGenerateAArch64(args):
  return (not args.aarch32_only and not args.aarch64_only) or args.aarch64_only

def GetAArch32Filename(test):
  return test.lower().replace('_', '-') + '.h'

def GetAArch64Filename(test):
  return test.lower().replace('_', '-') + '-trace-aarch64.h'

if __name__ == '__main__':
  # $ROOT/tools/generate_simulator_traces.py
  root_dir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
  os.chdir(root_dir)

  args = BuildOptions(root_dir)

  # List all tests.
  status, test_list = util.getstatusoutput(args.runner + ' --list')
  if status != 0: util.abort('Failed to list all tests')

  if ShouldGenerateAArch64(args):
    # Run each simulator test (AARCH64_SIM_*) with the --generate_test_trace
    # option, and use the output to create the traces header (from --out). In
    # addition, the test-simulator-traces-aarch64.h file, the master trace file,
    # which includes all other trace files is generated.

    # Create master trace file.
    master_trace_f = open(args.out, 'w')
    master_trace_f.write(copyright_header)
    master_trace_f.write(master_trace_header)
    master_trace_f.write('\n\n')

    # Find the AArch64 simulator tests.
    tests = sorted(filter(lambda t: 'AARCH64_SIM_' in t, test_list.split()),
                   key=lambda t: GetAArch64Filename(t))

    for test in tests:
      # Strip out 'AARCH64_' to get the name of the test.
      test_name = test[len('AARCH64_'):]
      trace_filename = GetAArch64Filename(test_name)
      if not args.filter or re.compile(args.filter).search(test):
        # Run each test.
        print 'Generating trace for ' + test;
        cmd = ' '.join([args.runner, '--generate_test_trace', test])
        status, output = util.getstatusoutput(cmd)
        if status != 0: util.abort('Failed to run ' + cmd + '.')

        # Create a new trace header file.
        trace_f =  open("test/aarch64/traces/" + trace_filename, 'w')
        trace_f.write(copyright_header)
        trace_f.write(trace_header)
        trace_f.write('\n')
        trace_f.write("#ifndef VIXL_" + test_name.upper() + "_TRACE_AARCH64_H_\n")
        trace_f.write("#define VIXL_" + test_name.upper() + "_TRACE_AARCH64_H_\n")
        trace_f.write('\n')
        trace_f.write(output)
        trace_f.write('\n')
        trace_f.write('\n' + "#endif  // VIXL_"
                      + test_name.upper() + "_TRACE_AARCH64_H_" + '\n')
        trace_f.close()

      # Update master trace file.
      master_trace_f.write(
          '#include \"aarch64/traces/' + trace_filename + '\"\n')

    # Close master trace file.
    master_trace_f.write(master_trace_footer)
    master_trace_f.close()

  if ShouldGenerateAArch32(args):
    # Run each test (AARCH32_{SIMULATOR,ASSEMBLER}_*) with the
    # --generate_test_trace option.

    # Find the AArch32 tests.
    tests = sorted(
        filter(
            lambda t: 'AARCH32_SIMULATOR_' in t or ('AARCH32_ASSEMBLER_' in t
                and not 'AARCH32_ASSEMBLER_NEGATIVE_' in t),
            test_list.split()),
        key=lambda t: GetAArch32Filename(t))
    if args.filter:
      tests = filter(re.compile(args.filter).search, tests)

    for test in tests:
      # Run each test.
      print 'Generating trace for ' + test;
      # Strip out 'AARCH32_' to get the name of the test.
      test_name = test[len('AARCH32_'):]

      # An "and" instruction will be called "and_" since we cannot clash with
      # the C++ operator. Rename "and_" to "and" to keep sane filenames.
      test_name = test_name.replace('and_', 'and')

      cmd = ' '.join([args.runner, '--generate_test_trace', test])
      status, output = util.getstatusoutput(cmd)
      if status != 0: util.abort('Failed to run ' + cmd + '.')

      # Create a new trace header file.
      trace_filename = GetAArch32Filename(test_name)
      trace_f =  open("test/aarch32/traces/" + trace_filename, 'w')
      trace_f.write(copyright_header)
      trace_f.write(trace_header)
      trace_f.write('\n')
      trace_f.write("#ifndef VIXL_" + test_name.upper() + "_H_\n")
      trace_f.write("#define VIXL_" + test_name.upper() + "_H_\n")
      trace_f.write('\n')
      trace_f.write(output)
      trace_f.write('\n')
      trace_f.write(
          '\n' + "#endif  // VIXL_" + test_name.upper() + "_H_" + '\n')
      trace_f.close()

  print 'Trace generation COMPLETE'