Commit bfd8bf5b authored by gaurav's avatar gaurav

Fixing indentation in mem.py, and a small bug in CFile.py

parent 71105773
#Define install directory
export MYBASEDIR:=/home/gaurav/eclipse-workspace/pycparser_examples
export MYBASEDIR:=/home/gaurav/eclipse-workspace/sls_thesis_project
#Directory for IR to C tool source
IR2CDIR=$(MYBASEDIR)/ir2c
......
......@@ -18,7 +18,7 @@ import mem
def process(sourceFile,fnHash):
labelStart = "bb "; # assuming that all labels names start with BB.
# Change if label prefix changes - Suhas, 31 March 2012
f=open(sourceFile+".125t.blocks");#".t99.blocks");
f=open(sourceFile+".127t.blocks");#".t99.blocks");
line=f.readline();
printFlag=1;
memFlag=0;
......@@ -154,4 +154,5 @@ def process(sourceFile,fnHash):
print line,
printFlag = 1;
line = f.readline()
return 1;
......@@ -74,18 +74,18 @@ def readGlobalData(sourceFile,fnHash):
m =re_functionLine.search(line);
if m == None:
m = re_functionLine2.search(line);
if m and (not fnAltArgStart):
m=re_functionDesc.match(m.group());
fnDesc=m.group();
m =re_functionName.search(fnDesc);
m = re.match('\w+',m.group());
fnName=m.group();
logging.debug("In main Fnmatch pattern, Fn = " + fnName)
if fnName in fnHash: # This could be a function within ifdef, which is finally not defined
if not (fnName in keywords.keywords):
fnHash[fnName].setDesc(fnDesc);
logging.debug("In main Fnmatch pattern, Fn desc = " + fnDesc)
stopPrint = 1;
if m and (not fnAltArgStart):
m=re_functionDesc.match(m.group());
fnDesc=m.group();
m =re_functionName.search(fnDesc);
m = re.match('\w+',m.group());
fnName=m.group();
logging.debug("In main Fnmatch pattern, Fn = " + fnName)
if fnName in fnHash: # This could be a function within ifdef, which is finally not defined
if not (fnName in keywords.keywords):
fnHash[fnName].setDesc(fnDesc);
logging.debug("In main Fnmatch pattern, Fn 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
......@@ -131,7 +131,7 @@ def readGlobalData(sourceFile,fnHash):
if stopPrint == 0:
#print "Debug. Printing lines from C file"
print line;
print line,;
line=f.readline();
......
......@@ -18,22 +18,22 @@ def memAccess(line,func):
if i%2==1:
line = line + a[i];
else:
if re_indirection.search(a[i]):
# Find pointer type for indirection operations
a[i]=resolveIndirection(a[i],func.getVars());
m=re_mem.split(a[i]);
a[i] = ""
for j in range(len(m)):
if j%2 == 1:
a[i] = a[i]+resolveMEM(m[j],func,a[0].strip());
else :
# Remove unnecessary pointer type casts
m[j]=removePtrCast(m[j]);
# Cast Ptrs where pointer arithmetic is taking place
m[j]=resolvePtrMath(m[j],func.getPtrs());
a[i] = a[i] + m[j];
# special handling for some bugs in IR operations
line = line + re_invert.sub('\g<1> = ~\g<1>', a[i])
if re_indirection.search(a[i]):
# Find pointer type for indirection operations
a[i]=resolveIndirection(a[i],func.getVars());
m=re_mem.split(a[i]);
a[i] = ""
for j in range(len(m)):
if j%2 == 1:
a[i] = a[i]+resolveMEM(m[j],func,a[0].strip());
else :
# Remove unnecessary pointer type casts
m[j]=removePtrCast(m[j]);
# Cast Ptrs where pointer arithmetic is taking place
m[j]=resolvePtrMath(m[j],func.getPtrs());
a[i] = a[i] + m[j];
# special handling for some bugs in IR operations
line = line + re_invert.sub('\g<1> = ~\g<1>', a[i])
return line;
......@@ -53,7 +53,7 @@ def resolveMEM(mem,func,lhs):
t = re.match('[ \w]+',mtype);
mtype=t.group();
if mtype.startswith('struct') and \
(m.group().find('.') >= 0 or m.group().find('->') >= 0):
(m.group().find('.') >= 0 or m.group().find('->') >= 0):
# struct member access, try to get type from LHS
m1 = re.match(r'\*\(([\w\s]+)\*\s*\)', lhs)
m2 = re.match(r'\(([\w\s]+)\)', lhs)
......@@ -71,31 +71,31 @@ def resolveMEM(mem,func,lhs):
mem="";
plus_sign=0;
for token in m:
t=re_tokensplit.split(token)
mem_components[t[0]]=t[1];
t=re_tokensplit.split(token)
mem_components[t[0]]=t[1];
if "symbol" in mem_components:
mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["symbol"])
plus_sign=1;
mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["symbol"])
plus_sign=1;
if "base" in mem_components:
if plus_sign:
mem=mem+" + "
mem= mem+resolvePtrMath(mem_components["base"],func.getPtrs(),1)
plus_sign=1;
if plus_sign:
mem=mem+" + "
mem= mem+resolvePtrMath(mem_components["base"],func.getPtrs(),1)
plus_sign=1;
if "index" in mem_components:
if plus_sign:
mem= mem+" + ";
mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["index"])
if plus_sign:
mem= mem+" + ";
mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["index"])
if "step" in mem_components:
step=re.match('\d+',mem_components["step"])
mem= mem+" * "+step.group();
plus_sign=1;
step=re.match('\d+',mem_components["step"])
mem= mem+" * "+step.group();
plus_sign=1;
if "offset" in mem_components:
mem = mem + " + "
mem_components["offset"] = mem_components["offset"].strip()
if mem_components["offset"].isdigit():
if int(mem_components["offset"]) >= 2**(TARGET_BITWIDTH-1):
mem = mem + "(int)"
mem= mem+mem_components["offset"];
mem = mem + " + "
mem_components["offset"] = mem_components["offset"].strip()
if mem_components["offset"].isdigit():
if int(mem_components["offset"]) >= 2**(TARGET_BITWIDTH-1):
mem = mem + "(int)"
mem= mem+mem_components["offset"];
mem="*("+mtype+"*)("+mem+")"
return mem;
......@@ -110,8 +110,8 @@ def resolveIndirection(istr, varHash):
re_dataType=re.compile(' \*|\[');#('\s*[^\w]'); # Regex to remove * , [nn] etc from var Type -- e.g int[64] becomes int
m= re_1.search(istr);
if m:
repl = "*"+m.group()[2:-3]+"*)(";
istr=re_indirect.sub(repl,istr);
repl = "*"+m.group()[2:-3]+"*)(";
istr=re_indirect.sub(repl,istr);
else:
m= re_2.search(istr)
if m:
......@@ -126,72 +126,72 @@ def resolveIndirection(istr, varHash):
return istr;
def removePtrCast(str):
def removePtrCast(string):
re_ptrCast=re.compile('\([^\(]+ \*\)');
str=re_ptrCast.sub("",str);
return str;
string=re_ptrCast.sub("",string);
return string;
def resolvePtrMath(str,ptrList,broad=0):
def resolvePtrMath(string,ptrList,broad=0):
re_var=re.compile('([\w\.]+)') #possible variable
re_fn=re.compile('(\w+\s*\(.*\))'); #function call ... not to be modified
re_word=re.compile('\w+');
m=re_fn.split(str);
str="";
m=re_fn.split(string);
string="";
for i in range(len(m)):
flag=1;
if i%2==1:
flag=0;
m1=re_word.match(m[i]);
if not (m1.group() in keywords.keywords):
str=str+m[i];
else:
flag=1;
flag=0;
m1=re_word.match(m[i]);
if not (m1.group() in keywords.keywords):
string=string+m[i];
else:
flag=1;
if flag:
if broad or (re.search('\S+\s+\S+',m[i])):
a=re_var.split(m[i]);
for j in range(len(a)):
# constant
if (j%2 == 1) and a[j].isdigit():
# if negative, make sure it is properly sign extended
if int(a[j]) >= 2**(TARGET_BITWIDTH-1):
str = str + "(int)"
# ptr variable
elif (j%2 == 1) and a[j] in ptrList:
# remove address-of operator
addr = 0
cast = 1
re_str=re.compile('(&)')
strTemp=re_str.split(str);
if len(strTemp) > 1 and not strTemp[-1].strip():
addr = 1
str = ''.join(strTemp[:-2])
# remove unnecessary (and wrong) pointer type cast
re_str=re.compile('(\([\w\s]+\*?\s*\))')
strTemp=re_str.split(str)
if len(strTemp) > 1 and not strTemp[-1].strip():
if strTemp[-2].find('*') >= 0:
cast = 0
else:
str = ''.join(strTemp[:-2])
# if it was plain array access, don't cast
if j+1 < len(a) and a[j+1].strip().startswith('['):
if not addr:
cast = 0
# if it was a struct member access, don't cast
if j+1 < len(a) and a[j+1].strip().startswith('->'):
if not addr:
cast = 0
# likewise if the pointer is immediately dereferenced
if str.strip().endswith('*'):
cast = 0
# create properly casted pointer-taking operation
if cast: str = str+UINTPTR_CAST
if addr: str = str+"&"
str=str+a[j];
else:
str=str+m[i];
return str;
if broad or (re.search('\S+\s+\S+',m[i])):
a=re_var.split(m[i]);
for j in range(len(a)):
# constant
if (j%2 == 1) and a[j].isdigit():
# if negative, make sure it is properly sign extended
if int(a[j]) >= 2**(TARGET_BITWIDTH-1):
string = string + "(int)"
# ptr variable
elif (j%2 == 1) and a[j] in ptrList:
# remove address-of operator
addr = 0
cast = 1
re_str=re.compile('(&)')
strTemp=re_str.split(string);
if len(strTemp) > 1 and not strTemp[-1].strip():
addr = 1
string = ''.join(strTemp[:-2])
# remove unnecessary (and wrong) pointer type cast
re_str=re.compile('(\([\w\s]+\*?\s*\))')
strTemp=re_str.split(string)
if len(strTemp) > 1 and not strTemp[-1].strip():
if strTemp[-2].find('*') >= 0:
cast = 0
else:
string = ''.join(strTemp[:-2])
# if it was plain array access, don't cast
if j+1 < len(a) and a[j+1].strip().startswith('['):
if not addr:
cast = 0
# if it was a struct member access, don't cast
if j+1 < len(a) and a[j+1].strip().startswith('->'):
if not addr:
cast = 0
# likewise if the pointer is immediately dereferenced
if string.strip().endswith('*'):
cast = 0
# create properly casted pointer-taking operation
if cast: string = string+UINTPTR_CAST
if addr: string = string+"&"
string=string+a[j];
else:
string=string+m[i];
return string;
......
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