xref: /XiangShan/scripts/coverage/coverage.py (revision 367512b707c976b7ff3fa2e0a4cf1b35a5c1d3c2)
1#/usr/bin/python3
2# -*- coding: UTF-8 -*-
3import sys
4import re
5import copy
6
7if __name__ == "__main__":
8    assert len(sys.argv) == 3, "Expect input_file and output_file"
9    input_file = sys.argv[1]
10    output_file = sys.argv[2]
11    lines = []
12    line_count = 0
13    synthesis_nest_level = 0
14    reg_init_nest_level = 0
15    mem_init_nest_level = 0
16    with open(input_file) as f:
17        for line in f:
18            line_count += 1
19
20            ifdef = re.compile('`ifdef')
21            ifndef = re.compile('`ifndef')
22            endif = re.compile('`endif')
23            # remove the line coverage results of not synthesizable code(mostly assert and fwrite)
24            synthesis = re.compile('`ifndef SYNTHESIS')
25            # remove the coverage results of random init variables
26            reg_init = re.compile('`ifdef RANDOMIZE_REG_INIT')
27            mem_init = re.compile('`ifdef RANDOMIZE_MEM_INIT')
28            coverage = re.compile('^\s*(%?\d+)\s+')
29
30
31            ifdef_match = ifdef.search(line)
32            ifndef_match = ifndef.search(line)
33            endif_match = endif.search(line)
34            synthesis_match = synthesis.search(line)
35            reg_init_match = reg_init.search(line)
36            mem_init_match = mem_init.search(line)
37            coverage_match = coverage.search(line)
38
39            # enter synthesis block
40            if synthesis_match:
41                assert synthesis_nest_level == 0, "Should not nest SYNTHESIS macro"
42                synthesis_nest_level = 1
43
44            if synthesis_nest_level > 0:
45                if ifdef_match or (ifndef_match and not synthesis_match):
46                    synthesis_nest_level += 1
47                if endif_match:
48                    synthesis_nest_level -= 1
49                    assert synthesis_nest_level >= 0, "Macro nest level should be >= 0"
50
51                # remove line coverage results in systhesis block
52                if coverage_match:
53                    coverage_stat = coverage_match.group(1)
54                    line = line.replace(coverage_match.group(1), " " * len(coverage_stat))
55
56            # enter reg_init block
57            if reg_init_match:
58                assert reg_init_nest_level == 0, "Should not nest reg_init macro"
59                reg_init_nest_level = 1
60
61            if reg_init_nest_level > 0:
62                if (ifdef_match and not reg_init_match) or ifndef_match:
63                    reg_init_nest_level += 1
64                if endif_match:
65                    reg_init_nest_level -= 1
66                    assert reg_init_nest_level >= 0, "Macro nest level should be >= 0"
67
68                # remove line coverage results in systhesis block
69                if coverage_match:
70                    coverage_stat = coverage_match.group(1)
71                    line = line.replace(coverage_match.group(1), " " * len(coverage_stat))
72
73            # enter mem_init block
74            if mem_init_match:
75                assert mem_init_nest_level == 0, "Should not nest mem_init macro"
76                mem_init_nest_level = 1
77
78            if mem_init_nest_level > 0:
79                if (ifdef_match and not mem_init_match) or ifndef_match:
80                    mem_init_nest_level += 1
81                if endif_match:
82                    mem_init_nest_level -= 1
83                    assert mem_init_nest_level >= 0, "Macro nest level should be >= 0"
84
85                # remove line coverage results in systhesis block
86                if coverage_match:
87                    coverage_stat = coverage_match.group(1)
88                    line = line.replace(coverage_match.group(1), " " * len(coverage_stat))
89
90            lines += line
91
92    with open(output_file, "w") as f:
93        for line in lines:
94            f.write(line)
95