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
f=open(sourceFile + '.055t.alias');
line = f.readline();
# Regular Expressions def process(sourceFile,fnHash):
re_fnStart = re.compile(';; Function'); f=open(sourceFile+ '.055t.alias');#'.t29.alias1');
re_word = re.compile('\w+'); line = f.readline();
re_varStart = re.compile('Referenced variables in '); re_fnStart=re.compile(';; Function');
re_varLine = re.compile('Variable:'); re_word=re.compile('\w+');
re_getVar = re.compile('Variable: |, |\n'); re_varStart=re.compile('Referenced variables in ');
var_read=0; re_varLine=re.compile('Variable:');
re_getVar=re.compile('Variable: |, |\n');
while line: var_read=0
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();
return 1;
f.close();
return 1;
\ No newline at end of file
This diff is collapsed.
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,132 +10,119 @@ import logging ...@@ -9,132 +10,119 @@ import logging
# Not completely verified though # Not completely verified though
def readGlobalData(sourceFile,fnHash): def readGlobalData(sourceFile,fnHash):
""" f=open(sourceFile);
Reads Global Data like comments, include macros and global declarations, line = f.readline();
which are not included in the Blocks File. stopPrint = 0;
""" comment= 0;
fnAltArgStart = 0;
f=open(sourceFile); fnName = ""
line = f.readline(); re_ppDirective=re.compile('\s*#')
stopPrint = 0; re_commentStart = re.compile('/\*');
comment= 0; re_commentEnd = re.compile('\*/');
fnAltArgStart = 0; re_comment = re.compile('\s*//');
fnName = "" re_functionLine = re.compile('[\w\s]*\w+\s+\w+\s*\(.*\)\s*($|\{)');
re_ppDirective=re.compile('\s*#') #pattern to match function with pointer return type.
re_commentStart = re.compile('/\*'); re_functionLine2 = re.compile('[\w\s]*\w+\s*\*\s*\w+\s*\(.*\)\s*($|\{)');
re_commentEnd = re.compile('\*/'); re_fnLineAltStart = re.compile('[\w\s]*\w+\s+\w+\s*\([^)]*$'); #multiline fn arg list
re_comment = re.compile('\s*//'); re_fnLineAltEnd = re.compile('.*\)\s*($|\{)');
re_functionLine = re.compile('[\w\s]*\w+\s+\w+\s*\(.*\)\s*($|\{)'); re_functionDesc = re.compile('.+\)') #remove comments or '{' from function line
# pattern to match function with pointer return type. re_functionName = re.compile('\w+\s*\(')
re_functionLine2 = re.compile('[\w\s]*\w+\s*\*\s*\w+\s*\(.*\)\s*($|\{)'); lineNum = 0
re_fnLineAltStart = re.compile('[\w\s]*\w+\s+\w+\s*\([^)]*$'); # multiline fn arg list commentLineNum = 0
re_fnLineAltEnd = re.compile('.*\)\s*($|\{)'); m = None
re_functionDesc = re.compile('.+\)') # remove comments or '{' from function line fnDesc_b4_Comment = 0
re_functionName = re.compile('\w+\s*\(')
lineNum = 0 while line:
commentLineNum = 0
m = None
fnDesc_b4_Comment = 0
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:
m = re_fnLineAltStart.search(line)
if m == None: if m == None:
m = re_fnLineAltStart.search(line) m = re_functionLine2.search(line);
if m == None:
m = re_functionLine2.search(line);
cM = re_commentStart.search(line) cM = re_commentStart.search(line)
if cM != None: if cM != None:
comment=1; comment=1;
commentLineNum = lineNum commentLineNum = lineNum
if m != None: if m != None:
if ( m.start() < cM.start() ): if ( m.start() < cM.start() ):
fnDesc_b4_Comment = 1 fnDesc_b4_Comment = 1
cS = re_comment.match(line) cS = re_comment.match(line)
if cS != None: if cS != None:
commentLineNum = lineNum commentLineNum = lineNum
if m != None: if m != None:
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); if m == None:
if m == None: m = re_functionLine2.search(line);
m = re_functionLine2.search(line); if m and (not fnAltArgStart):
if m and (not fnAltArgStart):
m=re_functionDesc.match(m.group()); m=re_functionDesc.match(m.group());
fnDesc=m.group(); fnDesc=m.group();
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,
if not (fnName in keywords.keywords): #which is finally not defined
fnHash[fnName].setDesc(fnDesc); if not (fnName in keywords.keywords):
logging.debug("In main Fnmatch pattern, Fn desc = " + fnDesc) fnHash[fnName].setDesc(fnDesc);
stopPrint = 1; #print"Debug. In main Fnmatch pattern\nFn desc = " + fnDesc
stopPrint = 1;
# This is part of multiline fn arg list pattern search.
# pushing it above the search for start of multiline arg list
# so that it doesn't repeat the recording of the first line of the list
if fnAltArgStart:
# still going through function arg list
if fnName in fnHash:
# This could be a function within ifdef,
# which is finally not defined, absent in Alias file
curFnDesc = fnHash[fnName].getDesc();
newDesc = curFnDesc + line;
fnHash[fnName].setDesc(newDesc);
m = re_fnLineAltStart.search(line); #This is part of multiline fn arg list pattern search.
# main pattern doesn't match multiline function arg list #pushing it above the search for start of multiline arg list
if m and (not fnAltArgStart): #so that it doesn't repeat the recording of the first line of the list
#This is an alternate search pattern if fnAltArgStart: #still going through function arg list
fnAltArgStart = 1; if fnName in fnHash: #This could be a function within ifdef,
#function arg list starts #which is finally not defined, absent in Alias file
curFnDesc = fnHash[fnName].getDesc();
newDesc = curFnDesc + line;
fnHash[fnName].setDesc(newDesc);
m = re_fnLineAltStart.search(line); #main pattern doesn't match multiline function arg list
if m and (not fnAltArgStart): #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); #add description line by line
fnHash[fnName].setDesc(line); #print"Debug. In Alt. Fnstartmatch pattern\nFn desc = " + fnHash[fnName].getDesc()
#add description line by line stopPrint = 1;
#print"Debug. In Alt. Fnstartmatch pattern\nFn desc = " + fnHash[fnName].getDesc()
stopPrint = 1;
m = re_fnLineAltEnd.search(line); #search for end of fn arg list m = re_fnLineAltEnd.search(line); #search for end of fn arg list
if m and fnAltArgStart: if m and fnAltArgStart:
fnAltArgStart = 0; #function arg list ends fnAltArgStart = 0; #function arg list ends
#if fnName in fnHash: #Debug #if fnName in fnHash: #Debug
# print"Debug. Fn desc = " + fnHash[fnName].getDesc() # print"Debug. Fn desc = " + fnHash[fnName].getDesc()
if re_commentEnd.search(line): if re_commentEnd.search(line):
comment=0; comment=0;
fnDesc_b4_Comment = 0 fnDesc_b4_Comment = 0
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();
return 1;
f.close();
return 1;
import re import re
class Function: class Function:
def __init__(self,name): def __init__(self,name):
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):
#print self.name;
def getDesc(self): return self.desc;
#print self.name; def getName(self):
return self.desc; return self.name;
def addVar(self,varName,varType):
def getName(self): self.varHash[varName]=varType;
return self.name;
def addVar(self,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):
return self.varHash;
def getPtrs(self):
return self.ptrList;
def getVars(self):
return self.varHash;
def getPtrs(self):
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);
This diff is collapsed.
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