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