Commit 37eafe49 authored by Gaurav Kukreja's avatar Gaurav Kukreja

Reverted all python files in ir2c code, to solve issues

Signed-off-by: Gaurav Kukreja's avatarGaurav Kukreja <gaurav@gauravk.in>
parent f2aa5106
# import sys import sys
import re import re
import fn import fn
import logging
def process(sourceFile, fnHash):
"""
Reads the '.alias' file generated by the compiler frontend, to create a hash
table of the local and global variables accessed by a function in the source
code.
"""
# Open alias file def process(sourceFile,fnHash):
f=open(sourceFile + '.055t.alias'); f=open(sourceFile+ '.055t.alias');#'.t29.alias1');
line = f.readline(); line = f.readline();
re_fnStart=re.compile(';; Function');
# Regular Expressions re_word=re.compile('\w+');
re_fnStart = re.compile(';; Function'); re_varStart=re.compile('Referenced variables in ');
re_word = re.compile('\w+'); re_varLine=re.compile('Variable:');
re_varStart = re.compile('Referenced variables in '); re_getVar=re.compile('Variable: |, |\n');
re_varLine = re.compile('Variable:'); var_read=0
re_getVar = re.compile('Variable: |, |\n');
var_read=0;
while line: while line:
if re_fnStart.match(line): if re_fnStart.match(line):
m = re_word.findall(line); m=re_word.findall(line);
fnName = m[1]; fnName=m[1];
fnHash[fnName] = fn.Function(fnName); fnHash[fnName]=fn.Function(fnName);
logging.debug("Function Name: " + fnName); #print fnName; #############debugging
if var_read==1: if var_read==1:
if re_varLine.match(line): if re_varLine.match(line):
m=re_getVar.split(line); m=re_getVar.split(line);
m[3]=re.sub(r'\{.*\}', '', m[3]); m[3]=re.sub(r'\{.*\}', '', m[3]);
logging.debug("\tVariable Name: " + m[1] +", Variable Type: " + m[3]); fnHash[fnName].addVar(m[1].strip(),m[3].strip());
fnHash[fnName].addVar(varName = m[1].strip(), varType = m[3].strip());
elif line.strip(): elif line.strip():
var_read=0; var_read=0;
if re_varStart.match(line): if re_varStart.match(line):
var_read=1; var_read=1;
line=f.readline(); line=f.readline();
f.close(); f.close();
return 1; return 1;
import re import re
# import sys import sys
# import fn import fn
import mem import mem
#20 Feb 2012 - modified var declaration end mark. There is no BLOCK 0. #20 Feb 2012 - modified var declaration end mark. There is no BLOCK 0.
#modified logic which sets memFlag. Earlier it wasn't getting set - Suhas #modified logic which sets memFlag. Earlier it wasn't getting set - Suhas
#31 Mar 2012 - added logic to preprocess block beginnings and convert #31 Mar 2012 - added logic to preprocess block beginnings and convert
...@@ -16,8 +15,8 @@ import mem ...@@ -16,8 +15,8 @@ import mem
#8-10 Oct 2012 - removing [*_expr] sort of patterns. #8-10 Oct 2012 - removing [*_expr] sort of patterns.
def process(sourceFile,fnHash): def process(sourceFile,fnHash):
labelStart = "bb "; # assuming that all labels names start with BB. labelStart = "bb "; #assuming that all labels names start with BB.
# Change if label prefix changes - Suhas, 31 March 2012 #Change if label prefix changes - Suhas, 31 March 2012
f=open(sourceFile+".127t.blocks");#".t99.blocks"); f=open(sourceFile+".127t.blocks");#".t99.blocks");
line=f.readline(); line=f.readline();
printFlag=1; printFlag=1;
...@@ -154,5 +153,4 @@ def process(sourceFile,fnHash): ...@@ -154,5 +153,4 @@ def process(sourceFile,fnHash):
print line, print line,
printFlag = 1; printFlag = 1;
line = f.readline() line = f.readline()
return 1; return 1;
import sys
import re import re
import fn
import keywords import keywords
import logging
#2 April 2012 - Original function line pattern doesn't work when fn args are #2 April 2012 - Original function line pattern doesn't work when fn args are
# in different lines. Changing logic to check for that # in different lines. Changing logic to check for that
...@@ -9,11 +10,6 @@ import logging ...@@ -9,11 +10,6 @@ import logging
# Not completely verified though # Not completely verified though
def readGlobalData(sourceFile,fnHash): def readGlobalData(sourceFile,fnHash):
"""
Reads Global Data like comments, include macros and global declarations,
which are not included in the Blocks File.
"""
f=open(sourceFile); f=open(sourceFile);
line = f.readline(); line = f.readline();
stopPrint = 0; stopPrint = 0;
...@@ -25,11 +21,11 @@ def readGlobalData(sourceFile,fnHash): ...@@ -25,11 +21,11 @@ def readGlobalData(sourceFile,fnHash):
re_commentEnd = re.compile('\*/'); re_commentEnd = re.compile('\*/');
re_comment = re.compile('\s*//'); re_comment = re.compile('\s*//');
re_functionLine = re.compile('[\w\s]*\w+\s+\w+\s*\(.*\)\s*($|\{)'); re_functionLine = re.compile('[\w\s]*\w+\s+\w+\s*\(.*\)\s*($|\{)');
# pattern to match function with pointer return type. #pattern to match function with pointer return type.
re_functionLine2 = re.compile('[\w\s]*\w+\s*\*\s*\w+\s*\(.*\)\s*($|\{)'); re_functionLine2 = re.compile('[\w\s]*\w+\s*\*\s*\w+\s*\(.*\)\s*($|\{)');
re_fnLineAltStart = re.compile('[\w\s]*\w+\s+\w+\s*\([^)]*$'); # multiline fn arg list re_fnLineAltStart = re.compile('[\w\s]*\w+\s+\w+\s*\([^)]*$'); #multiline fn arg list
re_fnLineAltEnd = re.compile('.*\)\s*($|\{)'); re_fnLineAltEnd = re.compile('.*\)\s*($|\{)');
re_functionDesc = re.compile('.+\)') # remove comments or '{' from function line re_functionDesc = re.compile('.+\)') #remove comments or '{' from function line
re_functionName = re.compile('\w+\s*\(') re_functionName = re.compile('\w+\s*\(')
lineNum = 0 lineNum = 0
commentLineNum = 0 commentLineNum = 0
...@@ -39,9 +35,8 @@ def readGlobalData(sourceFile,fnHash): ...@@ -39,9 +35,8 @@ def readGlobalData(sourceFile,fnHash):
while line: while line:
lineNum = lineNum + 1 lineNum = lineNum + 1
#if not in the middle of a multiline comment and not in the middle of
# if not in the middle of a multiline comment and not in the middle of #multiline fn arg list
# multiline fn arg list
if (comment == 0) and (not fnAltArgStart): if (comment == 0) and (not fnAltArgStart):
m =re_functionLine.search(line); m =re_functionLine.search(line);
if m == None: if m == None:
...@@ -64,11 +59,11 @@ def readGlobalData(sourceFile,fnHash): ...@@ -64,11 +59,11 @@ def readGlobalData(sourceFile,fnHash):
if ( m.start() < cS.start() ): if ( m.start() < cS.start() ):
fnDesc_b4_Comment = 1 fnDesc_b4_Comment = 1
if ( ((cS == None) and (comment == 0)) or \ if ( ((cS == None) and (comment == 0)) or \
((commentLineNum == lineNum) and (fnDesc_b4_Comment)) )\ ((commentLineNum == lineNum) and (fnDesc_b4_Comment)) )\
and (not re_ppDirective.match(line)): and (not re_ppDirective.match(line)):
# in function params, if only "short" or "long" is mentioned,
# replaces it with "short int" or "long int" respectively.
line = re.sub(r'(^|\s)long(\s+)(?!(int|double))', '\g<1>long int\g<2>', line) line = re.sub(r'(^|\s)long(\s+)(?!(int|double))', '\g<1>long int\g<2>', line)
line = re.sub(r'(^|\s)short(\s+)(?!(int|double))', '\g<1>short int\g<2>', line) line = re.sub(r'(^|\s)short(\s+)(?!(int|double))', '\g<1>short int\g<2>', line)
m =re_functionLine.search(line); m =re_functionLine.search(line);
...@@ -80,41 +75,36 @@ def readGlobalData(sourceFile,fnHash): ...@@ -80,41 +75,36 @@ def readGlobalData(sourceFile,fnHash):
m =re_functionName.search(fnDesc); m =re_functionName.search(fnDesc);
m = re.match('\w+',m.group()); m = re.match('\w+',m.group());
fnName=m.group(); fnName=m.group();
logging.debug("In main Fnmatch pattern, Fn = " + fnName) #print"Debug. In main Fnmatch pattern\nFn = " + fnName
if fnName in fnHash: # This could be a function within ifdef, which is finally not defined if fnName in fnHash: #This could be a function within ifdef,
#which is finally not defined
if not (fnName in keywords.keywords): if not (fnName in keywords.keywords):
fnHash[fnName].setDesc(fnDesc); fnHash[fnName].setDesc(fnDesc);
logging.debug("In main Fnmatch pattern, Fn desc = " + fnDesc) #print"Debug. In main Fnmatch pattern\nFn desc = " + fnDesc
stopPrint = 1; stopPrint = 1;
# This is part of multiline fn arg list pattern search.
# pushing it above the search for start of multiline arg list #This is part of multiline fn arg list pattern search.
# so that it doesn't repeat the recording of the first line of the list #pushing it above the search for start of multiline arg list
if fnAltArgStart: #so that it doesn't repeat the recording of the first line of the list
# still going through function arg list if fnAltArgStart: #still going through function arg list
if fnName in fnHash: if fnName in fnHash: #This could be a function within ifdef,
# This could be a function within ifdef, #which is finally not defined, absent in Alias file
# which is finally not defined, absent in Alias file
curFnDesc = fnHash[fnName].getDesc(); curFnDesc = fnHash[fnName].getDesc();
newDesc = curFnDesc + line; newDesc = curFnDesc + line;
fnHash[fnName].setDesc(newDesc); fnHash[fnName].setDesc(newDesc);
m = re_fnLineAltStart.search(line); m = re_fnLineAltStart.search(line); #main pattern doesn't match multiline function arg list
# main pattern doesn't match multiline function arg list if m and (not fnAltArgStart): #This is an alternate search pattern
if m and (not fnAltArgStart): fnAltArgStart = 1; #function arg list starts
#This is an alternate search pattern
fnAltArgStart = 1;
#function arg list starts
m = re_functionName.search(m.group()); m = re_functionName.search(m.group());
m = re.match('\w+',m.group()); m = re.match('\w+',m.group());
fnName=m.group(); fnName=m.group();
#print"Debug. In Alt. Fnstartmatch pattern \nFn = " + fnName #print"Debug. In Alt. Fnstartmatch pattern \nFn = " + fnName
if fnName in fnHash: if fnName in fnHash: #This could be a function within ifdef,
#This could be a function within ifdef,
#which is finally not defined #which is finally not defined
if not (fnName in keywords.keywords): if not (fnName in keywords.keywords):
fnHash[fnName].setDesc(line); fnHash[fnName].setDesc(line); #add description line by line
#add description line by line
#print"Debug. In Alt. Fnstartmatch pattern\nFn desc = " + fnHash[fnName].getDesc() #print"Debug. In Alt. Fnstartmatch pattern\nFn desc = " + fnHash[fnName].getDesc()
stopPrint = 1; stopPrint = 1;
...@@ -132,9 +122,7 @@ def readGlobalData(sourceFile,fnHash): ...@@ -132,9 +122,7 @@ def readGlobalData(sourceFile,fnHash):
if stopPrint == 0: if stopPrint == 0:
#print "Debug. Printing lines from C file" #print "Debug. Printing lines from C file"
print line,; print line,;
line=f.readline(); line=f.readline();
f.close(); f.close();
return 1; return 1;
...@@ -5,24 +5,20 @@ class Function: ...@@ -5,24 +5,20 @@ class Function:
self.name=name; self.name=name;
self.varHash={}; self.varHash={};
self.ptrList=[]; self.ptrList=[];
def setDesc(self,desc): def setDesc(self,desc):
self.desc=desc; self.desc=desc;
def getDesc(self): def getDesc(self):
#print self.name; #print self.name;
return self.desc; return self.desc;
def getName(self): def getName(self):
return self.name; return self.name;
def addVar(self,varName,varType): def addVar(self,varName,varType):
self.varHash[varName]=varType; self.varHash[varName]=varType;
if re.search('[\*\[]',varType): if re.search('[\*\[]',varType):
self.ptrList.append(varName); self.ptrList.append(varName);
def getVars(self): def getVars(self):
return self.varHash; return self.varHash;
def getPtrs(self): def getPtrs(self):
return self.ptrList; return self.ptrList;
import sys import sys
import re
import CFile import CFile
import BlocksFile import BlocksFile
import fn
import AliasFile import AliasFile
sourceFile = sys.argv[1]; sourceFile = sys.argv[1];
...@@ -17,8 +19,7 @@ print "" ...@@ -17,8 +19,7 @@ print ""
print " ***********************************************************/" print " ***********************************************************/"
print "#include <limits.h>" print "#include <limits.h>"
print "#include <stdint.h>" print "#include <stdint.h>"
print "#include <ir2c.h>" print "#include \"ir2c.h\""
print "" print ""
CFile.readGlobalData(sourceFile,fnHash); CFile.readGlobalData(sourceFile,fnHash);
BlocksFile.process(sourceFile,fnHash); BlocksFile.process(sourceFile,fnHash);
import re import re
import fn
import keywords import keywords
TARGET_BITWIDTH = 32 TARGET_BITWIDTH = 32
...@@ -42,7 +43,7 @@ def resolveMEM(mem,func,lhs): ...@@ -42,7 +43,7 @@ def resolveMEM(mem,func,lhs):
varHash = func.getVars() varHash = func.getVars()
re_memsplit=re.compile('\w+: [^,|^\]]+'); #('\w+: [\w|\.]+') re_memsplit=re.compile('\w+: [^,|^\]]+'); #('\w+: [\w|\.]+')
re_tokensplit=re.compile(': '); re_tokensplit=re.compile(': ');
# re_plus=re.compile('\+\s*(\([\w\s]+\))?'); re_plus=re.compile('\+\s*(\([\w\s]+\))?');
re_cast=re.compile('\s*\([\w\s]+\)') re_cast=re.compile('\s*\([\w\s]+\)')
mem_components={}; mem_components={};
# resolve type # resolve type
...@@ -125,25 +126,25 @@ def resolveIndirection(istr, varHash): ...@@ -125,25 +126,25 @@ def resolveIndirection(istr, varHash):
return istr; return istr;
def removePtrCast(string): def removePtrCast(str):
re_ptrCast=re.compile('\([^\(]+ \*\)'); re_ptrCast=re.compile('\([^\(]+ \*\)');
string=re_ptrCast.sub("",string); str=re_ptrCast.sub("",str);
return string; return str;
def resolvePtrMath(string,ptrList,broad=0): def resolvePtrMath(str,ptrList,broad=0):
re_var=re.compile('([\w\.]+)') #possible variable re_var=re.compile('([\w\.]+)') #possible variable
re_fn=re.compile('(\w+\s*\(.*\))'); #function call ... not to be modified re_fn=re.compile('(\w+\s*\(.*\))'); #function call ... not to be modified
re_word=re.compile('\w+'); re_word=re.compile('\w+');
m=re_fn.split(string); m=re_fn.split(str);
string=""; str="";
for i in range(len(m)): for i in range(len(m)):
flag=1; flag=1;
if i%2==1: if i%2==1:
flag=0; flag=0;
m1=re_word.match(m[i]); m1=re_word.match(m[i]);
if not (m1.group() in keywords.keywords): if not (m1.group() in keywords.keywords):
string=string+m[i]; str=str+m[i];
else: else:
flag=1; flag=1;
if flag: if flag:
...@@ -154,25 +155,25 @@ def resolvePtrMath(string,ptrList,broad=0): ...@@ -154,25 +155,25 @@ def resolvePtrMath(string,ptrList,broad=0):
if (j%2 == 1) and a[j].isdigit(): if (j%2 == 1) and a[j].isdigit():
# if negative, make sure it is properly sign extended # if negative, make sure it is properly sign extended
if int(a[j]) >= 2**(TARGET_BITWIDTH-1): if int(a[j]) >= 2**(TARGET_BITWIDTH-1):
string = string + "(int)" str = str + "(int)"
# ptr variable # ptr variable
elif (j%2 == 1) and a[j] in ptrList: elif (j%2 == 1) and a[j] in ptrList:
# remove address-of operator # remove address-of operator
addr = 0 addr = 0
cast = 1 cast = 1
re_str=re.compile('(&)') re_str=re.compile('(&)')
strTemp=re_str.split(string); strTemp=re_str.split(str);
if len(strTemp) > 1 and not strTemp[-1].strip(): if len(strTemp) > 1 and not strTemp[-1].strip():
addr = 1 addr = 1
string = ''.join(strTemp[:-2]) str = ''.join(strTemp[:-2])
# remove unnecessary (and wrong) pointer type cast # remove unnecessary (and wrong) pointer type cast
re_str=re.compile('(\([\w\s]+\*?\s*\))') re_str=re.compile('(\([\w\s]+\*?\s*\))')
strTemp=re_str.split(string) strTemp=re_str.split(str)
if len(strTemp) > 1 and not strTemp[-1].strip(): if len(strTemp) > 1 and not strTemp[-1].strip():
if strTemp[-2].find('*') >= 0: if strTemp[-2].find('*') >= 0:
cast = 0 cast = 0
else: else:
string = ''.join(strTemp[:-2]) str = ''.join(strTemp[:-2])
# if it was plain array access, don't cast # if it was plain array access, don't cast
if j+1 < len(a) and a[j+1].strip().startswith('['): if j+1 < len(a) and a[j+1].strip().startswith('['):
if not addr: if not addr:
...@@ -182,15 +183,15 @@ def resolvePtrMath(string,ptrList,broad=0): ...@@ -182,15 +183,15 @@ def resolvePtrMath(string,ptrList,broad=0):
if not addr: if not addr:
cast = 0 cast = 0
# likewise if the pointer is immediately dereferenced # likewise if the pointer is immediately dereferenced
if string.strip().endswith('*'): if str.strip().endswith('*'):
cast = 0 cast = 0
# create properly casted pointer-taking operation # create properly casted pointer-taking operation
if cast: string = string+UINTPTR_CAST if cast: str = str+UINTPTR_CAST
if addr: string = string+"&" if addr: str = str+"&"
string=string+a[j]; str=str+a[j];
else: else:
string=string+m[i]; str=str+m[i];
return string; return str;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment