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
306
|
#!/usr/bin/env python3
#
# Copyright © 2020 Google, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from mako.template import Template
from isa import ISA
import argparse
import os
import sys
template = """\
/* Copyright (C) 2020 Google, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "${header}"
/*
* enum tables, these don't have any link back to other tables so just
* dump them up front before the bitset tables
*/
%for name, enum in isa.enums.items():
static const struct isa_enum ${enum.get_c_name()} = {
.num_values = ${len(enum.values)},
.values = {
% for val, display in enum.values.items():
{ .val = ${val}, .display = "${display}" },
% endfor
},
};
%endfor
/*
* generated expression functions, can be linked from bitset tables, so
* also dump them up front
*/
%for name, expr in isa.expressions.items():
static uint64_t
${expr.get_c_name()}(struct decode_scope *scope)
{
% for fieldname in sorted(expr.fieldnames):
int64_t ${fieldname} = isa_decode_field(scope, "${fieldname}");
% endfor
return ${expr.expr};
}
%endfor
/*
* Forward-declarations (so we don't have to figure out which order to
* emit various tables when they have pointers to each other)
*/
%for name, bitset in isa.all_bitsets():
static const struct isa_bitset bitset_${bitset.get_c_name()}_gen_${bitset.gen_min};
%endfor
%for root_name, root in isa.roots.items():
const struct isa_bitset *${root.get_c_name()}[];
%endfor
/*
* bitset tables:
*/
%for name, bitset in isa.all_bitsets():
% for case in bitset.cases:
% for field_name, field in case.fields.items():
% if field.get_c_typename() == 'TYPE_BITSET':
% if len(field.params) > 0:
static const struct isa_field_params ${case.get_c_name()}_gen_${bitset.gen_min}_${field.get_c_name()} = {
.num_params = ${len(field.params)},
.params = {
% for param in field.params:
{ .name= "${param[0]}", .as = "${param[1]}" },
% endfor
},
};
% endif
% endif
% endfor
static const struct isa_case ${case.get_c_name()}_gen_${bitset.gen_min} = {
% if case.expr is not None:
.expr = &${isa.expressions[case.expr].get_c_name()},
% endif
% if case.display is not None:
.display = "${case.display}",
% endif
.num_fields = ${len(case.fields)},
.fields = {
% for field_name, field in case.fields.items():
{ .name = "${field_name}", .low = ${field.low}, .high = ${field.high},
% if field.expr is not None:
.expr = &${isa.expressions[field.expr].get_c_name()},
% endif
% if field.display is not None:
.display = "${field.display}",
% endif
.type = ${field.get_c_typename()},
% if field.get_c_typename() == 'TYPE_BITSET':
.bitsets = ${isa.roots[field.type].get_c_name()},
% if len(field.params) > 0:
.params = &${case.get_c_name()}_gen_${bitset.gen_min}_${field.get_c_name()},
% endif
% endif
% if field.get_c_typename() == 'TYPE_ENUM':
.enums = &${isa.enums[field.type].get_c_name()},
% endif
% if field.get_c_typename() == 'TYPE_ASSERT':
.val.bitset = { ${', '.join(isa.split_bits(field.val, 32))} },
% endif
},
% endfor
},
};
% endfor
static const struct isa_bitset bitset_${bitset.get_c_name()}_gen_${bitset.gen_min} = {
<% pattern = bitset.get_pattern() %>
% if bitset.extends is not None:
.parent = &bitset_${isa.bitsets[bitset.extends].get_c_name()}_gen_${isa.bitsets[bitset.extends].gen_min},
% endif
.name = "${name}",
.gen = {
.min = ${bitset.get_gen_min()},
.max = ${bitset.get_gen_max()},
},
.match.bitset = { ${', '.join(isa.split_bits(pattern.match, 32))} },
.dontcare.bitset = { ${', '.join(isa.split_bits(pattern.dontcare, 32))} },
.mask.bitset = { ${', '.join(isa.split_bits(pattern.mask, 32))} },
.num_cases = ${len(bitset.cases)},
.cases = {
% for case in bitset.cases:
&${case.get_c_name()}_gen_${bitset.gen_min},
% endfor
},
};
%endfor
/*
* bitset hierarchy root tables (where decoding starts from):
*/
%for root_name, root in isa.roots.items():
const struct isa_bitset *${root.get_c_name()}[] = {
% for leaf_name, leafs in isa.leafs.items():
% for leaf in leafs:
% if leaf.get_root() == root:
&bitset_${leaf.get_c_name()}_gen_${leaf.gen_min},
% endif
% endfor
% endfor
(void *)0
};
%endfor
#include "isaspec_decode_impl.c"
"""
header = """\
/* Copyright (C) 2020 Google, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef _${guard}_
#define _${guard}_
#include <stdint.h>
#include <util/bitset.h>
#define BITMASK_WORDS BITSET_WORDS(${isa.bitsize})
typedef struct {
BITSET_WORD bitset[BITMASK_WORDS];
} bitmask_t;
#define BITSET_FORMAT ${isa.format()}
#define BITSET_VALUE(v) ${isa.value()}
static inline void
next_instruction(bitmask_t *instr, BITSET_WORD *start)
{
%for i in range(0, int(isa.bitsize / 32)):
instr->bitset[${i}] = *(start + ${i});
%endfor
}
static inline uint64_t
bitmask_to_uint64_t(bitmask_t mask)
{
% if isa.bitsize <= 32:
return mask.bitset[0];
% else:
return ((uint64_t)mask.bitset[1] << 32) | mask.bitset[0];
% endif
}
static inline bitmask_t
uint64_t_to_bitmask(uint64_t val)
{
bitmask_t mask = {
.bitset[0] = val & 0xffffffff,
% if isa.bitsize > 32:
.bitset[1] = (val >> 32) & 0xffffffff,
% endif
};
return mask;
}
#include "isaspec_decode_decl.h"
#endif /* _${guard}_ */
"""
def guard(p):
return os.path.basename(p).upper().replace("-", "_").replace(".", "_")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--xml', required=True, help='isaspec XML file.')
parser.add_argument('--out-c', required=True, help='Output C file.')
parser.add_argument('--out-h', required=True, help='Output H file.')
args = parser.parse_args()
isa = ISA(args.xml)
try:
with open(args.out_c, 'w') as f:
out_h_basename = os.path.basename(args.out_h)
f.write(Template(template).render(isa=isa, header=out_h_basename))
with open(args.out_h, 'w') as f:
f.write(Template(header).render(isa=isa, guard=guard(args.out_h)))
except Exception:
# In the event there's an error, this imports some helpers from mako
# to print a useful stack trace and prints it, then exits with
# status 1, if python is run with debug; otherwise it just raises
# the exception
import sys
from mako import exceptions
print(exceptions.text_error_template().render(), file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()
|