updated for pep8 conformity

This commit is contained in:
Jan Laukemann
2017-09-24 16:49:26 +02:00
parent 19b5b3d74c
commit bfba024db7

View File

@@ -11,26 +11,24 @@ sem = 0
db = {}
sorted_db = []
lncnt = 1
#cnt=0
fname = ""
cntChar = ''
first = True
def extract_instr(asmFile):
global once
global lncnt
global fname
fname = asmFile
#Check if parameter is in the correct file format
# Check if parameter is in the correct file format
if(asmFile[-4:] != ".log"):
print("Invalid argument")
sys.exit()
#Open file
# Open file
try:
f=open(asmFile, "r")
except IOError:
print("IOError: File not found")
#Analyse code line by line and check the instructions
# Analyse code line by line and check the instructions
lncnt = 1
for line in f:
check_line(line)
@@ -42,30 +40,29 @@ def check_line(line):
global numSeps
global sem
global first
#Check if marker is in line and count the number of whitespaces if so
# Check if marker is in line and count the number of whitespaces if so
if(marker in line):
#But first, check if high level code ist indented with whitespaces or tabs
# But first, check if high level code ist indented with whitespaces or tabs
if(first):
set_counter_char(line)
first = False
numSeps = (re.split(marker,line)[0]).count(cntChar)
sem = 2;
elif(sem > 0):
#We're in the marked code snipped
#Check if the line is ASM code and - if not - check if we're still in the loop
# We're in the marked code snipped
# Check if the line is ASM code and - if not - check if we're still in the loop
match = re.search(asm_line, line)
if(match):
#Further analysis of instructions
# print("".join(re.split(r'\t',line)[-1:]),end="")
#Check if there are commetns in line
# Further analysis of instructions
# Check if there are commetns in line
if(r'//' in line):
return
check_instr("".join(re.split(r'\t',line)[-1:]))
elif((re.split(r'\S',line)[0]).count(cntChar) <= numSeps):
#Not in the loop anymore - or yet - so we decrement the semaphore
# Not in the loop anymore - or yet - so we decrement the semaphore
sem = sem-1
#Check if seperator is either tabulator or whitespace
# Check if seperator is either tabulator or whitespace
def set_counter_char(line):
global cntChar
numSpaces = (re.split(marker,line)[0]).count(" ")
@@ -75,7 +72,8 @@ def set_counter_char(line):
elif(numSpaces == 0 and numTabs != 0):
cntChar = '\t'
else:
raise NotImplementedError("Indentation of code is only supported for whitespaces and tabs.")
raise NotImplementedError("Indentation of code is only supported for whitespaces and
tabs.")
def check_instr(instr):
@@ -83,20 +81,21 @@ def check_instr(instr):
global lncnt
global cnt
global fname
#Check for strange clang padding bytes
# Check for strange clang padding bytes
while(instr.startswith("data32")):
instr = instr[7:]
#Seperate mnemonic and operands
# Seperate mnemonic and operands
mnemonic = instr.split()[0]
params = "".join(instr.split()[1:])
#Check if line is not only a byte
# Check if line is not only a byte
empty_byte = re.compile(r'[0-9a-f]{2}')
if(re.match(empty_byte, mnemonic) and len(mnemonic) == 2):
return
#Check if there's one or more operand and store all in a list
# Check if there's one or more operand and store all in a list
param_list = flatten(separate_params(params))
opList = list(param_list)
#Check operands and seperate them by IMMEDIATE (IMD), REGISTER (REG), MEMORY (MEM) or LABEL (LBL)
# Check operands and seperate them by IMMEDIATE (IMD), REGISTER (REG), MEMORY (MEM) or
# LABEL (LBL)
for i in range(len(param_list)):
op = param_list[i]
if(len(op) <= 0):
@@ -116,44 +115,28 @@ def check_instr(instr):
op = MemAddr(op)
param_list[i] = str(op)
opList[i] = op
#Join mnemonic and operand(s) to an instruction form
# Join mnemonic and operand(s) to an instruction form
if(len(mnemonic) > 7):
tabs = "\t"
else:
tabs = "\t\t"
instr_form = mnemonic+tabs+(" ".join(param_list))
#Check in database for instruction form and increment the counter
# Check in database for instruction form and increment the counter
if(instr_form in db):
db[instr_form] = db[instr_form]+1
else:
db[instr_form] = 1
#Create testcase for instruction form, since it is the first appearance of it
#But (as far as now) only for instr forms with only registers as operands
# is_Reg = True
# for par in opList:
# print(par.print()+" is Register: "+str(isinstance(par, Register)))
# if(not isinstance(par, Register)):
# is_Reg = False
# if(is_Reg):
#print(mnemonic)
# print("create testcase for "+mnemonic+" with params:")
# for p in opList:
# print(p.print(),end=", ")
# print()
#Only create benchmark if no label (LBL) is part of the operands
# Create testcase for instruction form, since it is the first appearance of it
# Only create benchmark if no label (LBL) is part of the operands
do_bench = True
for par in opList:
if(str(par) == 'LBL' or str(par) == ''):
do_bench = False
if(do_bench):
#Create testcase with reversed param list, due to the fact its intel syntax!
# create_testcase(mnemonic, list(reversed(opList)))
# print('menmonic: '+mnemonic+' ops: '+str(list(reversed(opList))))
# Create testcase with reversed param list, due to the fact its intel syntax!
tc = Testcase(mnemonic, list(reversed(opList)), '64')
tc.write_testcase()
# print("-----------")
def separate_params(params):
param_list = [params]
@@ -181,17 +164,16 @@ def sort_db():
def print_sorted_db():
sort_db()
sum = 0
total = 0
print("Number of\tmnemonic")
print("calls\n")
for i in range(len(sorted_db)):
print(str(sorted_db[i][1])+"\t\t"+sorted_db[i][0])
sum += sorted_db[i][1]
print("\nCumulated number of instructions: "+str(sum))
total += sorted_db[i][1]
print("\nCumulated number of instructions: "+str(total))
def save_db():
global db
file = open(".cnt_asm_ops.db","w")
for i in db.items():
file.write(i[0]+"\t"+str(i[1])+"\n")
@@ -207,7 +189,7 @@ def load_db():
return
for line in file:
mnemonic = line.split('\t')[0]
#Join mnemonic and operand(s) to an instruction form
# Join mnemonic and operand(s) to an instruction form
if(len(mnemonic) > 7):
tabs = "\t"
params = line.split('\t')[1]
@@ -230,18 +212,17 @@ def flatten(l):
if __name__ == "__main__":
# load_db()
# r0 = Register("ymm0")
# r1 = Register("xmm0")
# r64 = Register("rax")
# r32 = Register("eax")
# mem0 = MemAddr('(%rax, %esi, 4)')
# tc = Testcase("XOR", [r32, r32], '64')
# tc.write_testcase()
# create_testcase("VADDPD", [r0, r0, r0])
# load_db()
# r0 = Register("ymm0")
# r1 = Register("xmm0")
# r64 = Register("rax")
# r32 = Register("eax")
# mem0 = MemAddr('(%rax, %esi, 4)')
# tc = Testcase("XOR", [r32, r32], '64')
# tc.write_testcase()
# create_testcase("VADDPD", [r0, r0, r0])
if(len(sys.argv) > 1):
for i in range(1,len(sys.argv)):
extract_instr(sys.argv[i])
print_sorted_db()
# save_db()