File size: 6,770 Bytes
6705032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405c83f
6705032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405c83f
6705032
0cea2c9
6705032
 
 
 
 
 
 
 
 
0cea2c9
6705032
 
 
 
 
 
 
 
 
 
 
 
 
0cea2c9
6705032
 
 
0cea2c9
6705032
 
 
 
 
 
 
 
 
 
 
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
# -*- encoding: utf-8 -*-

 
from asyncore import write
import My_Difflib
from distutils.command.config import config
import os, sys,string,re,glob
import html
import csv
import copy
from tqdm import tqdm
import pathlib

folder = str(pathlib.Path(__file__).parent.resolve())

Wrong_Dir = folder
Mips_Dir = folder + "/Mips_Code"
VEGA_Dir = folder + "/VEGA_Code"
Ans_Dir = folder + "/Std_Code"
count_wrong = 0

Mod_File_Func = {}
Mod_Result = {}



def deal_dir(src_dir):
    global Mod_File_Func
    global Mod_Result
    if not os.path.exists(src_dir):
        return False
    filelists = os.listdir(src_dir)
    for eachfile in filelists:
        tem =eachfile
        eachfile = src_dir + '/' +eachfile
        if os.path.isdir(eachfile):
            deal_dir(eachfile)
        elif os.path.isfile(eachfile) and ".cpp" in eachfile:
            module = eachfile.split("/")[-3]
            if module not in Mod_Result.keys():
                Mod_Result[module] = [0,0,0]
            file = eachfile.split("/")[-2]
            func = eachfile.split("/")[-1].replace(".cpp", "")
            if (" ").join([module, file, func]) not in Mod_File_Func.keys():
                Mod_File_Func[(" ").join([module, file, func])] = []
            codes = ""
            f2 = open(eachfile, encoding = "utf-8")
            lines = f2.readlines()
            
            for idx, l in enumerate(lines):
                if idx == len(lines) - 1:
                    codes += l.replace(" ", "").replace("\n", "").lower().replace("mips","").replace("xcore", "").replace("pulp", "").replace("riscv", "")
                else:
                    codes += l.replace(" ", "").lower().replace("mips","").replace("xcore", "").replace("pulp", "").replace("riscv", "")
            f2.close()
            
            if "/Mips" in eachfile:
                Mod_File_Func[(" ").join([module, file, func])].append(["Mips", codes])
            elif "/Std_Code/" in eachfile:
                Mod_File_Func[(" ").join([module, file, func])].append(["Ans", codes])
            elif "/VEGA" in eachfile:
                Mod_File_Func[(" ").join([module, file, func])].append(["VEGA", codes])
    return True

wrong_stmt = []
def get_wrong_list():
    global wrong_stmt
    with open(Wrong_Dir+"/wrong_list_all.csv", 'r', encoding='utf-8') as fcsv:
        reader = csv.reader(fcsv)
        for row in reader:
            if row[0] == "idx":
                continue
            wrong_stmt.append(row[0].strip().lower() + " " + row[1].strip().lower() + " " + row[2].strip().lower())
               



def duplicate_data(tar):
    global wrong_stmt
    global Mod_File_Func
    global Mod_Result

    for kv in Mod_File_Func.items():
        module = kv[0].split(" ")[0]
        if len(kv[1]) != 3:
            print(tar + " " + kv[0])
        Ans = ""
        Vega = ""
        Mips = ""
        
        for codes in kv[1]:
            if codes[0] == "Mips":
                Mips = codes[1]
                #print(codes[1])
            if codes[0] == "Ans":
                Ans = codes[1]
            if codes[0] == "VEGA":
                Vega = codes[1]
        
        total = len(Ans.split("\n"))
        #print(Mod_Result)
        Mod_Result[module][0] += total

        vega_same = 0
        vega_modi = 0
        vega_add = 0
        Mips_same = 0
        Mips_modi = 0
        Mips_add = 0
        diff_vega = list(My_Difflib.Differ().compare(Ans.splitlines(), Vega.splitlines()))
        
        for idx, dv in enumerate(diff_vega):
            if dv[0] == '-':
                if idx < len(diff_vega) - 1 and diff_vega[idx+1][0] == '?':
                    vega_modi += 1
                else:
                    vega_add += 1
            elif dv[0] == '+':
                continue
            elif dv[0] == '?':
                continue
                #vega_add -= 1
            elif dv.strip().replace("\n", "") == '':
                continue
            else:
                vega_same += 1

        if kv[0].split(" ")[-2].strip().lower() + " " + kv[0].split(" ")[-1].strip().lower() + " " + tar.lower() not in wrong_stmt:
            vega_same = total
            vega_modi = 0
            vega_add = 0
        Mod_Result[module][2] += vega_same
        if Mips.replace("\n", "") != "None":
            diff_mips = list(My_Difflib.Differ().compare(Ans.splitlines(), Mips.splitlines()))
            for dm in diff_mips:
                if dm[0] == '-':
                    if idx < len(diff_mips) - 1 and diff_mips[idx+1][0] == '?':
                        Mips_modi += 1
                    else:
                        Mips_add += 1
                elif dm[0] == '+':
                    continue
                elif dm[0] == '?':
                    continue
                    #vega_add -= 1
                elif dm.strip().replace("\n", "") == '':
                    continue
                else:
                    Mips_same += 1
        else:
            Mips_add = total
        
        Mod_Result[module][1] +=  Mips_same


    with open(folder+"/Fig9.csv", 'a', encoding='utf-8', newline="") as f:
        f_csv = csv.writer(f)
        avg_vega = 0.0
        avg_mips = 0.0
        for kv in Mod_Result.items():
            f_csv.writerow([tar.replace("PULP", "RI5CY"), kv[0], round(kv[1][1]*1.0 / kv[1][0], 3), round(kv[1][2]*1.0 / kv[1][0], 3)])
            avg_vega += float(round(kv[1][2]*1.0 / kv[1][0], 3))
            avg_mips += float(round(kv[1][1]*1.0 / kv[1][0], 3))
        f_csv.writerow([tar.replace("PULP", "RI5CY"), "Avg", round(avg_mips / len(Mod_Result), 3), round(avg_vega / len(Mod_Result), 3)])

    with open(folder+"/Table3.csv", 'a', encoding='utf-8', newline="") as f:
        f_csv = csv.writer(f)
        all_vega = 0
        all_mips = 0
        all_stmt = 0
        for kv in Mod_Result.items():
            f_csv.writerow([tar.replace("PULP", "RI5CY"), kv[0], kv[1][2], kv[1][0]-kv[1][2]])
            all_vega += kv[1][2]
            all_mips += kv[1][1]
            all_stmt += kv[1][0]
        f_csv.writerow([tar.replace("PULP", "RI5CY"), "All", all_vega, all_stmt - all_vega])

if __name__ == '__main__':
    get_wrong_list()
    with open(folder+"/Fig9.csv", 'w', encoding='utf-8', newline="") as f:
        f_csv = csv.writer(f)
        f_csv.writerow(["Target", "Module", "Fork_Acc", "VEGA_Acc"])

    with open(folder+"/Table3.csv", 'w', encoding='utf-8', newline="") as f:
        f_csv = csv.writer(f)
        f_csv.writerow(["Target", "Module", "VEGA_Accurate_Lines", "VEGA_Manual_Lines"])

    for tar in ["XCore", "PULP", "RISCV"]:
        Mod_File_Func = {}
        Mod_Result = {}
        deal_dir(Mips_Dir+"/"+tar)
        deal_dir(VEGA_Dir+"/"+tar)
        deal_dir(Ans_Dir+"/"+tar)

        duplicate_data(tar)