xref: /XiangShan/scripts/coverage/coverage.py (revision 708ceed4afe43fb0ea3a52407e46b2794c573634)
1#/usr/bin/python3
2# -*- coding: UTF-8 -*-
3
4#***************************************************************************************
5# Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
6# Copyright (c) 2020-2021 Peng Cheng Laboratory
7#
8# XiangShan is licensed under Mulan PSL v2.
9# You can use this software according to the terms and conditions of the Mulan PSL v2.
10# You may obtain a copy of Mulan PSL v2 at:
11#          http://license.coscl.org.cn/MulanPSL2
12#
13# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
14# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
15# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
16#
17# See the Mulan PSL v2 for more details.
18#***************************************************************************************
19
20import sys
21import re
22import copy
23
24if __name__ == "__main__":
25    assert len(sys.argv) == 3, "Expect input_file and output_file"
26    input_file = sys.argv[1]
27    output_file = sys.argv[2]
28    lines = []
29    line_count = 0
30    synthesis_nest_level = 0
31    reg_init_nest_level = 0
32    mem_init_nest_level = 0
33    with open(input_file) as f:
34        for line in f:
35            line_count += 1
36
37            ifdef = re.compile('`ifdef')
38            ifndef = re.compile('`ifndef')
39            endif = re.compile('`endif')
40            # remove the line coverage results of not synthesizable code(mostly assert and fwrite)
41            synthesis = re.compile('`ifndef SYNTHESIS')
42            # remove the coverage results of random init variables
43            reg_init = re.compile('`ifdef RANDOMIZE_REG_INIT')
44            mem_init = re.compile('`ifdef RANDOMIZE_MEM_INIT')
45            coverage = re.compile('^\s*(%?\d+)\s+')
46
47
48            ifdef_match = ifdef.search(line)
49            ifndef_match = ifndef.search(line)
50            endif_match = endif.search(line)
51            synthesis_match = synthesis.search(line)
52            reg_init_match = reg_init.search(line)
53            mem_init_match = mem_init.search(line)
54            coverage_match = coverage.search(line)
55
56            # enter synthesis block
57            if synthesis_match:
58                assert synthesis_nest_level == 0, "Should not nest SYNTHESIS macro"
59                synthesis_nest_level = 1
60
61            if synthesis_nest_level > 0:
62                if ifdef_match or (ifndef_match and not synthesis_match):
63                    synthesis_nest_level += 1
64                if endif_match:
65                    synthesis_nest_level -= 1
66                    assert synthesis_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 reg_init block
74            if reg_init_match:
75                assert reg_init_nest_level == 0, "Should not nest reg_init macro"
76                reg_init_nest_level = 1
77
78            if reg_init_nest_level > 0:
79                if (ifdef_match and not reg_init_match) or ifndef_match:
80                    reg_init_nest_level += 1
81                if endif_match:
82                    reg_init_nest_level -= 1
83                    assert reg_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            # enter mem_init block
91            if mem_init_match:
92                assert mem_init_nest_level == 0, "Should not nest mem_init macro"
93                mem_init_nest_level = 1
94
95            if mem_init_nest_level > 0:
96                if (ifdef_match and not mem_init_match) or ifndef_match:
97                    mem_init_nest_level += 1
98                if endif_match:
99                    mem_init_nest_level -= 1
100                    assert mem_init_nest_level >= 0, "Macro nest level should be >= 0"
101
102                # remove line coverage results in systhesis block
103                if coverage_match:
104                    coverage_stat = coverage_match.group(1)
105                    line = line.replace(coverage_match.group(1), " " * len(coverage_stat))
106
107            lines += line
108
109    with open(output_file, "w") as f:
110        for line in lines:
111            f.write(line)
112