forked from tropicsquare/ts-spect-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_cover_class.py
executable file
·331 lines (259 loc) · 15.7 KB
/
generate_cover_class.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
# -*- coding: utf-8 -*-
####################################################################################################
# Generator of coverage class from instruction definitions.
#
# TODO: License
####################################################################################################
__author__ = "Ondrej Ille"
__copyright__ = "Tropic Square"
__license___ = "TODO:"
__maintainer__ = "Ondrej Ille"
import sys
import jinja2
from jinja2 import Template
from argparse import ArgumentParser
DPI_I_NAME = "dpi_instruction"
def form_val_code_sample(cov_point_name, has_op1, has_op2, has_op3, has_r31, has_special_cp, special_cov_point_name, special_op):
rv = " for (int i=0; i<256; i++) begin\n"
if (has_op1):
rv += f" {cov_point_name}[1].sample(op1, i);\n"
if (has_op2):
rv += f" {cov_point_name}[2].sample(op2, i);\n"
if (has_op3):
rv += f" {cov_point_name}[3].sample(op3, i);\n"
if (has_r31):
rv += f" {cov_point_name}[4].sample(r31, i);\n"
if (has_special_cp):
rv += f" {special_cov_point_name}.sample({special_op}, i);\n"
rv += " end\n"
return rv
def form_imm_sample(cov_point_name):
rv = " for (int i=0; i<12; i++) begin\n"
rv += f" {cov_point_name}.sample(dpi_instruction.immediate, i);\n"
rv += " end\n"
return rv
def form_addr_sample(cov_point_name):
rv = " for (int i=0; i<16; i++) begin\n"
rv += f" {cov_point_name}.sample(dpi_instruction.addr, i);\n"
rv += " end\n"
return rv
def form_newpc_sample(cov_point_name):
rv = " for (int i=0; i<16; i++) begin\n"
rv += f" {cov_point_name}.sample(dpi_instruction.new_pc, i);\n"
rv += " end\n"
return rv
def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text # or whatever
if __name__ == "__main__":
parser = ArgumentParser(description="Coverage class generator for SPECT.")
parser.add_argument("--input", help="Input file with instruction definitions.")
parser.add_argument("--output", help="Output file where to generate SPECT coverage class.")
parser.add_argument("--cov-template", help="Path to coverage template file.")
parser.add_argument("--instr-defs", help="Path to instruction definitions.")
args = parser.parse_args()
# Open Jinja template
with open(args.cov_template) as fd_tf_:
template_ = Template(fd_tf_.read())
i_ctx = {
"instructions" : []
}
# Load Instruction definitions
with open(args.instr_defs) as fd_instr_:
lines = fd_instr_.readlines()
i_types = []
for line in lines:
# On first line load available instruction types
if (line.startswith("_TYPES:")):
raw = line.strip("\n").split(":")[1].strip(' ').split(" ")
#print(raw)
for i_type in raw:
tmp = i_type.split("=")
i_types.append([tmp[0], tmp[1]])
print("Loaded instruction types: ")
print(i_types)
# Check for valid line
skip_line = True
for i_type in i_types:
if line[0] == i_type[0]:
skip_line = False
if skip_line:
continue
# Parse out Instruction attribtues
[i_type_str, mnemonic, opcode, func, op_mask, r31_dep, c_time, op2_op3_cross, cycles] = line.split()
i_type_int = 0
for i_type in i_types:
if (i_type[0] == i_type_str):
i_type_int = i_type[1]
func = remove_prefix(func, "0b")
opcode = remove_prefix(opcode, "0b")
i_type_int = remove_prefix(i_type_int, "0b")
# Parse instruction type and generate names of cover groups
op_cov_def = ""
val_cov_def = ""
res_val_cov_def = ""
op3_val_cov_def = ""
imm_cov_def = ""
newpc_cov_def = ""
cross_cov_def = ""
op_cov_sample = ""
val_cov_sample = ""
imm_cov_sample = ""
newpc_cov_sample = ""
cross_cov_sample = ""
addr_cov_def = ""
addr_cov_sample = ""
if (i_type_str == "R"):
if op_mask == "0b111":
if mnemonic == "ADDP" or mnemonic == "SUBP":
op_cov_def = f" operands_3_regs_modular_cov m_{mnemonic}_instruction_ops_cov;\n"
else:
op_cov_def = f" operands_3_regs_cov m_{mnemonic}_instruction_ops_cov;\n"
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op1, {DPI_I_NAME}.op2, {DPI_I_NAME}.op3);\n"
if r31_dep == "true":
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:4];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, True, True, True, False, "", "")
else:
if mnemonic == "ADD" or mnemonic == "SUB":
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[2:3];\n"
res_val_cov_def = f" register_arith_cov m_{mnemonic}_res_reg_cov;\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", False, True, True, False, True, f"m_{mnemonic}_res_reg_cov", "op1")
elif mnemonic == "SBIT" or mnemonic == "CBIT":
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:2];\n"
op3_val_cov_def = f" register_bit_cov m_{mnemonic}_op3_reg_cov;\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, True, False, False, True, f"m_{mnemonic}_op3_reg_cov", "op3")
elif mnemonic == "MUL25519":
val_cov_def = f" register_mul25519_cov m_{mnemonic}_reg_cov[1:3];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, True, True, False, False, "", "")
else:
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:3];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, True, True, False, False, "", "")
if op2_op3_cross == "1":
if mnemonic == "MUL25519":
cross_cov_def = f" op2_op3_cross_mul25519_cov m_{mnemonic}_op_cross_cov;\n"
else:
cross_cov_def = f" op2_op3_cross_cov m_{mnemonic}_op_cross_cov;\n"
cross_cov_sample = f" m_{mnemonic}_op_cross_cov.sample(op2, op3);\n"
elif (op_mask == "0b011" or op_mask == "0b110" or op_mask == "0b101"):
op_cov_def = f" operands_2_regs_cov m_{mnemonic}_instruction_ops_cov;\n"
if (op_mask == "0b011"):
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[2:3];\n"
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op2, {DPI_I_NAME}.op3);\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", False, True, True, False, False, "", "")
elif (op_mask == "0b110"):
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op1, {DPI_I_NAME}.op2);\n"
if mnemonic == "LSR" or mnemonic == "LSL":
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[2:2];\n"
res_val_cov_def = f" register_{mnemonic.lower()}_cov m_{mnemonic}_res_reg_cov;\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", False, True, False, False, True, f"m_{mnemonic}_res_reg_cov", "op1")
else:
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:2];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, True, False, False, False, "", "")
else:
print(f"Skipping operand coverpoint for: {mnemonic}")
elif (op_mask == "0b100" or op_mask == "0b010" or op_mask == "0b001"):
op_cov_def = f" operands_1_reg_cov m_{mnemonic}_instruction_ops_cov;\n"
if (op_mask == "0b100"):
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op1);\n"
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:1];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, False, False, False, False, "", "")
elif (op_mask == "0b010"):
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op2);\n"
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[2:2];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", False, True, False, False, False, "", "")
elif (op_mask == "0b001"):
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op3);\n"
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[3:3];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", False, False, True, False, False, "", "")
#else:
# print(f"Invalid operand mask: {op_mask}, for instruction: {mnemonic}")
elif (i_type_str == "I"):
if op_mask == "0b111":
op_cov_def = f" operands_2_regs_cov m_{mnemonic}_instruction_ops_cov;\n"
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op1, {DPI_I_NAME}.op2);\n"
imm_cov_def = f" immediate_cov m_{mnemonic}_imm_cov;\n"
imm_cov_sample = form_imm_sample(f"m_{mnemonic}_imm_cov")
if mnemonic == "ADDI" or mnemonic == "SUBI":
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[2:2];\n"
res_val_cov_def = f" register_arith_cov m_{mnemonic}_res_reg_cov;\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", False, True, False, False, True, f"m_{mnemonic}_res_reg_cov", "op1")
else:
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:2];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, True, False, False, False, "", "")
elif (op_mask == "0b011" or op_mask == "0b101"):
op_cov_def = f" operands_1_reg_cov m_{mnemonic}_instruction_ops_cov;\n"
if (op_mask == "0b101"):
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op1);\n"
if mnemonic == "MOVI":
val_cov_def = f" register_movi_cov m_{mnemonic}_reg_cov[1:1];\n"
else:
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:1];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, False, False, False, False, "", "")
else:
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op2);\n"
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[2:2];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", False, True, False, False, False, "", "")
imm_cov_def = f" immediate_cov m_{mnemonic}_imm_cov;\n"
imm_cov_sample = form_imm_sample(f"m_{mnemonic}_imm_cov")
#else:
# print(f"Invalid operand mask: {op_mask}, for instruction: {mnemonic}")
elif (i_type_str == "M"):
op_cov_def = f" operands_1_reg_cov m_{mnemonic}_instruction_ops_cov;\n"
op_cov_sample = f" m_{mnemonic}_instruction_ops_cov.sample({DPI_I_NAME}.op1);\n"
val_cov_def = f" register_cov m_{mnemonic}_reg_cov[1:1];\n"
val_cov_sample = form_val_code_sample(f"m_{mnemonic}_reg_cov", True, False, False, False, False, "", "")
addr_cov_def = f" addr_newpc_cov m_{mnemonic}_addr_cov;\n"
addr_cov_sample = form_addr_sample(f"m_{mnemonic}_addr_cov")
elif (i_type_str == "J"):
if op_mask == "0b100":
newpc_cov_def = f" addr_newpc_cov m_{mnemonic}_newpc_cov;\n"
newpc_cov_sample = form_newpc_sample(f"m_{mnemonic}_newpc_cov")
new_instr = {
"mnemonic" : mnemonic,
"i_type" : i_type_int,
"opcode" : opcode,
"func" : func,
"op_mask" : op_mask,
"r31_dep" : r31_dep,
"c_time " : c_time,
}
if op_cov_def:
new_instr["op_cov_def"] = op_cov_def
new_instr["op_cov_constr"] = f" m_{mnemonic}_instruction_ops_cov = new;\n"
new_instr["op_cov_sample"] = op_cov_sample
if val_cov_def:
new_instr["val_cov_def"] = val_cov_def
new_instr["val_cov_constr"] = f" foreach (m_{mnemonic}_reg_cov[i]) m_{mnemonic}_reg_cov[i] = new;\n"
new_instr["val_cov_sample"] = val_cov_sample
if res_val_cov_def:
new_instr["res_val_cov_def"] = res_val_cov_def
new_instr["res_val_cov_constr"] = f" m_{mnemonic}_res_reg_cov = new;\n"
if op3_val_cov_def:
new_instr["op3_val_cov_def"] = op3_val_cov_def
new_instr["op3_val_cov_constr"] = f" m_{mnemonic}_op3_reg_cov = new;\n"
if imm_cov_def:
new_instr["imm_cov_def"] = imm_cov_def
new_instr["imm_cov_constr"] = f" m_{mnemonic}_imm_cov = new;\n"
new_instr["imm_cov_sample"] = imm_cov_sample
# Accounts for both, NewPC and Address
if newpc_cov_def:
new_instr["newpc_cov_def"] = newpc_cov_def
new_instr["newpc_cov_constr"] = f" m_{mnemonic}_newpc_cov = new;\n"
new_instr["newpc_cov_sample"] = newpc_cov_sample
if addr_cov_def:
new_instr["addr_cov_def"] = addr_cov_def
new_instr["addr_cov_constr"] = f" m_{mnemonic}_addr_cov = new;\n"
new_instr["addr_cov_sample"] = addr_cov_sample
if cross_cov_def:
new_instr["cross_cov_def"] = cross_cov_def
new_instr["cross_cov_constr"] = f" m_{mnemonic}_op_cross_cov = new;\n"
new_instr["cross_cov_sample"] = cross_cov_sample
# Add to context to be passed to Jinja
i_ctx["instructions"].append(new_instr)
# Open output file
with open(args.output, "w") as fd_of_:
fd_of_.write(template_.render(i_ctx))
sys.exit(0)