Merge pull request #460 from bwelton/inst_fix
[dyninst.git] / instructionAPI / aarch64_pseudocode_extractor.py
1 import os, sys
2 import re
3
4 ISA_dir = '/p/paradyn/arm/arm-download-1350222/AR100-DA-70000-r0p0-00rel10/AR100-DA-70000-r0p0-00rel10/ISA_xml/ISA_xml_v2_00rel11/'
5 ISA_ps = '/p/paradyn/development/ssunny/dyninst/dyninst-code/instructionAPI/ISA_ps/'
6 files_dir = os.listdir(ISA_dir)
7 ifkeywords = ["else", "elsif"]
8 lineignorewords = ["Constrain", "CheckSPAlignment"]
9
10 for f in files_dir:
11     parts = f.split('.')
12     if len(parts) != 2 or parts[1] != "xml" or len(parts[0]) < 1:
13         continue
14     instr = parts[0]
15
16     curfname = ISA_dir + instr + ".xml"
17     fdata = ""
18     with open(curfname) as curfile:
19         fdata = curfile.read().replace('\n', '')
20     if fdata.find("aliasto") != -1:
21         refiformval = fdata.split("aliasto ")[1].split(" ")[0]
22         curfname = ISA_dir + refiformval.split("=")[1].replace('\"', '')
23
24     lines = list(open(curfname))
25     idx = -1
26
27     startPs = False
28     isDecode = False
29     isExecute = False
30
31     blockidxstack = list()
32     blockcount = 0
33     
34     decode = ""
35     execute = ""
36     for line in lines:
37         idx += 1
38
39         if line.find("<ps ") != -1:
40             startPs = True
41         # elif line.find("section=\"Decode\"") != -1:
42         #    isDecode = True
43         #    decode += line
44         elif line.find("section=\"Execute\"") != -1:
45             isExecute = True
46             execute += line
47             if (line.find("if") != -1 and line.find("then") != -1) or (line.find("when") != -1):
48                 blockcount = blockcount + 1
49                 blockidxstack.append(curidx)
50         elif line.find("</ps>") != -1:
51             startPs = False
52             # if isDecode == True:
53             #   isDecode = False
54             if isExecute == True:
55                 isExecute = False
56                 while blockcount > 0:
57                     top = blockidxstack.pop()
58                     execute += (" " * top)
59                     execute += "end\n"
60                     blockcount -= 1
61         elif startPs == True:
62             # if isDecode == True:
63             #   decode += line
64             if isExecute == True and any(kw in line for kw in lineignorewords) == False:
65                 curidx = len(line) - len(line.lstrip())
66                 
67                 if blockcount > 0 and (blockidxstack[len(blockidxstack) - 1] >= curidx or line.strip() == "") and any(kw in line for kw in ifkeywords) == False:
68                     top = blockidxstack.pop()
69                     
70                     prevline = lines[idx - 1]
71                     if prevline.find("if") == -1 and prevline.find("then") == -1:
72                         execute += (" " * top)
73                         execute += "end\n"
74                     blockcount -= 1
75
76                 if (line.find("if") != -1 and line.find("then") != -1 and line.find("bits") != 0 and line.find("elsif") == -1) or (line.find("when") != -1):
77                     blockcount = blockcount + 1
78                     blockidxstack.append(curidx)
79                 
80                 execute += line
81             else:
82                 print("!!!Found pseudocode section that is not decode or execute!!!")
83             # sys.exit(0)
84         else:
85             continue
86
87     decode = re.sub(r"<[^>]*>|^\s+|\'", "", decode)
88     decode = re.sub(r"&amp;", "&", re.sub(r"&gt;", ">", re.sub(r"&lt;", "<", decode)))
89     execute = re.sub(r"&amp;", "&", re.sub(r"&gt;", ">", re.sub(r"&lt;", "<", re.sub(r"<[^>]*>|^\s+|\'", "", execute))))
90     execute = re.sub(r"Zeros\(\)", "Zeros(64)", execute)
91
92     f = open(ISA_ps + instr, "w")
93     if decode != "":
94         f.write("##" + instr + "_" + "decode\n")
95         f.write(decode)
96         f.write("@@\n")
97     if execute != "":
98         f.write("##" + instr + "_" + "execute\n")
99         f.write(execute)
100         f.write("@@\n")
101     f.close()