forked from intelxed/xed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchipmodel.py
executable file
·306 lines (262 loc) · 9.6 KB
/
chipmodel.py
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# -*- python -*-
#BEGIN_LEGAL
#
#Copyright (c) 2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#END_LEGAL
from __future__ import print_function
import sys
import os
import re
import enum_txt_writer
import codegen
import genutil
def _die(s):
genutil.die(s)
def filter_comments(lines):
n = []
for line in lines:
t = re.sub('#.*','',line)
t = t.strip()
if t:
n.append(t)
return n
all_of_pattern = re.compile(r'ALL_OF[(](?P<chip>[A-Z0-9a-z_]+)[)]')
not_pattern = re.compile(r'NOT[(](?P<ext>[A-Z0-9a-z_]+)[)]')
common_subset_pattern = re.compile(r'COMMON_SUBSET[(](?P<chip1>[A-Z0-9a-z_]+),(?P<chip2>[A-Z0-9a-z_]+)[)]')
def uniquify_list(l):
d = {}
for a in l:
d[a]=True
return list(d.keys())
def expand_common_subset(d):
"""return true to keep going, false otherwise"""
found = False
for chip,ext_list in d.items():
newexts = []
for ext in ext_list:
m = common_subset_pattern.match(ext)
if m:
found = True
chip1 = m.group('chip1')
chip2 = m.group('chip2')
exts1 = set(d[chip1])
exts2 = set(d[chip2])
common = exts1.intersection(exts2)
newexts.extend(list(common))
else:
newexts.append(ext)
d[chip] = uniquify_list(newexts)
return found
def expand_all_of_once(d):
"""return true to keep going, false otherwise"""
found = False
for chip,ext_list in d.items():
newexts = []
for ext in ext_list:
m = all_of_pattern.match(ext)
if m:
found = True
other_chip = m.group('chip')
newexts.extend(d[other_chip])
else:
newexts.append(ext)
d[chip] = uniquify_list(newexts)
return found
def expand_macro(d,expander):
found = True
while found:
found = expander(d)
def expand_macro_not(d):
for chip,ext_list in d.items():
to_remove = []
positive_exts = []
for ext in ext_list:
m = not_pattern.match(ext)
if m:
to_remove.append( m.group('ext'))
else:
positive_exts.append(ext)
for r in to_remove:
try:
positive_exts.remove(r)
except:
_die("Could not remove %s from %s for chip %s" %
( r, " ".join(positive_exts), chip))
d[chip] = uniquify_list(positive_exts)
def parse_lines(input_file_name, lines): # returns a dictionary
"""Return a list of chips and a dictionary indexed by chip containing
lists of isa-sets """
d = {}
chips = []
for line in lines:
if line.find(':') == -1:
_die("reading file %s. " +
"Missing colon in line: %s" %
(input_file_name, line))
try:
(chip, extensions) = line.split(':')
except:
_die("Bad line: {}".format(line))
chip = chip.strip()
chips.append(chip)
extensions = extensions.split()
if chip in d:
_die("Duplicate definition of %s in %s" %
(chip, input_file_name))
if chip == 'ALL':
_die("Cannot define a chip named 'ALL'." +
" That name is reserved.")
d[chip] = extensions
return (chips,d)
def _feature_index(all_features, f):
try:
return all_features.index(f)
except:
_die("Did not find isa set %s in list\n" % (f))
def read_database(filename):
lines = open(filename,'r').readlines()
lines = filter_comments(lines)
lines = genutil.process_continuations(lines)
# returns a list and a dictionary
(chips,chip_features_dict) = parse_lines(filename,lines)
expand_macro(chip_features_dict,expand_all_of_once)
expand_macro(chip_features_dict,expand_common_subset)
expand_macro_not(chip_features_dict)
return (chips,chip_features_dict)
def _format_names(lst):
cols = 4
lines = ('\t'.join(lst[i:i+cols]) for i in range(0,len(lst),cols))
return '\n\t'.join(lines)
def dump_chip_hierarchy(arg, chips, chip_features_dict):
fe = codegen.xed_file_emitter_t(arg.xeddir,
arg.gendir,
'cdata.txt',
shell_file=True)
fe.start(full_header=False)
for c in chips:
fl = chip_features_dict[c]
fl.sort()
s = "{} :\n".format(c)
s = s + '\t' + _format_names(fl) + '\n'
fe.write(s)
fe.close()
return fe.full_file_name
def work(arg):
(chips,chip_features_dict) = read_database(arg.input_file_name)
isa_set_per_chip_fn = dump_chip_hierarchy(arg, chips, chip_features_dict)
# the XED_CHIP_ enum
chips.append("ALL")
chip_enum = enum_txt_writer.enum_info_t(['INVALID'] + chips,
arg.xeddir,
arg.gendir,
'xed-chip',
'xed_chip_enum_t',
'XED_CHIP_',
cplusplus=False)
chip_enum.print_enum()
chip_enum.run_enumer()
# Add the "ALL" chip
# the XED_ISA_SET_ enum
isa_set = set()
for vl in list(chip_features_dict.values()):
for v in vl:
isa_set.add(v.upper())
isa_set = list(isa_set)
isa_set.sort()
chip_features_dict['ALL'] = isa_set
isa_set = ['INVALID'] + isa_set
isa_set_enum = enum_txt_writer.enum_info_t(isa_set,
arg.xeddir,
arg.gendir,
'xed-isa-set',
'xed_isa_set_enum_t',
'XED_ISA_SET_',
cplusplus=False)
isa_set_enum.print_enum()
isa_set_enum.run_enumer()
# the initialization file and header
chip_features_cfn = 'xed-chip-features-table.c'
chip_features_hfn = 'xed-chip-features-table.h'
cfe = codegen.xed_file_emitter_t(arg.xeddir,
arg.gendir,
chip_features_cfn,
shell_file=False)
private_gendir = os.path.join(arg.gendir,'include-private')
hfe = codegen.xed_file_emitter_t(arg.xeddir,
private_gendir,
chip_features_hfn,
shell_file=False)
for header in [ 'xed-isa-set-enum.h', 'xed-chip-enum.h' ]:
cfe.add_header(header)
hfe.add_header(header)
cfe.start()
hfe.start()
cfe.write("xed_uint64_t xed_chip_features[XED_CHIP_LAST][4];\n")
hfe.write("extern xed_uint64_t xed_chip_features[XED_CHIP_LAST][4];\n")
fo = codegen.function_object_t('xed_init_chip_model_info', 'void')
fo.add_code_eol("const xed_uint64_t one=1")
# make a set for each machine name
spacing = "\n |"
for c in chips:
s0 = ['0']
s1 = ['0']
s2 = ['0']
s3 = ['0']
# loop over the features
for f in chip_features_dict[c]:
feature_index = _feature_index(isa_set,f)
if feature_index < 64:
s0.append('(one<<XED_ISA_SET_%s)' % (f))
elif feature_index < 128:
s1.append('(one<<(XED_ISA_SET_%s-64))' % (f))
elif feature_index < 192:
s2.append('(one<<(XED_ISA_SET_%s-128))' % (f))
elif feature_index < 256:
s3.append('(one<<(XED_ISA_SET_%s-192))' % (f))
else:
_die("Feature index > 256. Need anotehr features array")
s0s = spacing.join(s0)
s1s = spacing.join(s1)
s2s = spacing.join(s2)
s3s = spacing.join(s3)
for i,x in enumerate([s0s, s1s, s2s,s3s]):
fo.add_code_eol("xed_chip_features[XED_CHIP_{}][{}] = {}".format(c,i,x) )
cfe.write(fo.emit())
cfe.close()
hfe.write(fo.emit_header())
hfe.close()
return ( [ isa_set_per_chip_fn,
chip_enum.hdr_full_file_name,
chip_enum.src_full_file_name,
isa_set_enum.hdr_full_file_name,
isa_set_enum.src_full_file_name,
hfe.full_file_name,
cfe.full_file_name],
chips, isa_set)
class args_t(object):
def __init__(self):
self.input_file_name = None
self.xeddir = None
self.gendir = None
if __name__ == '__main__':
arg = args_t()
arg.input_file_name = 'datafiles/xed-chips.txt'
arg.xeddir = '.'
arg.gendir = 'obj'
files_created,chips,isa_set = work(arg)
print("Created files: %s" % (" ".join(files_created)))
sys.exit(0)