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 #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 #Directory for IR to C tool source
IR2CDIR=$(MYBASEDIR)/ir2c IR2CDIR=$(MYBASEDIR)/ir2c
......
...@@ -18,7 +18,7 @@ import mem ...@@ -18,7 +18,7 @@ import mem
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+".125t.blocks");#".t99.blocks"); f=open(sourceFile+".127t.blocks");#".t99.blocks");
line=f.readline(); line=f.readline();
printFlag=1; printFlag=1;
memFlag=0; memFlag=0;
...@@ -154,4 +154,5 @@ def process(sourceFile,fnHash): ...@@ -154,4 +154,5 @@ def process(sourceFile,fnHash):
print line, print line,
printFlag = 1; printFlag = 1;
line = f.readline() line = f.readline()
return 1; return 1;
...@@ -74,18 +74,18 @@ def readGlobalData(sourceFile,fnHash): ...@@ -74,18 +74,18 @@ def readGlobalData(sourceFile,fnHash):
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) 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 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) logging.debug("In main Fnmatch pattern, Fn desc = " + fnDesc)
stopPrint = 1; stopPrint = 1;
# This is part of multiline fn arg list pattern search. # This is part of multiline fn arg list pattern search.
# pushing it above the search for start of multiline arg list # pushing it above the search for start of multiline arg list
...@@ -131,7 +131,7 @@ def readGlobalData(sourceFile,fnHash): ...@@ -131,7 +131,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();
......
...@@ -18,22 +18,22 @@ def memAccess(line,func): ...@@ -18,22 +18,22 @@ def memAccess(line,func):
if i%2==1: if i%2==1:
line = line + a[i]; line = line + a[i];
else: else:
if re_indirection.search(a[i]): if re_indirection.search(a[i]):
# Find pointer type for indirection operations # Find pointer type for indirection operations
a[i]=resolveIndirection(a[i],func.getVars()); a[i]=resolveIndirection(a[i],func.getVars());
m=re_mem.split(a[i]); m=re_mem.split(a[i]);
a[i] = "" a[i] = ""
for j in range(len(m)): for j in range(len(m)):
if j%2 == 1: if j%2 == 1:
a[i] = a[i]+resolveMEM(m[j],func,a[0].strip()); a[i] = a[i]+resolveMEM(m[j],func,a[0].strip());
else : else :
# Remove unnecessary pointer type casts # Remove unnecessary pointer type casts
m[j]=removePtrCast(m[j]); m[j]=removePtrCast(m[j]);
# Cast Ptrs where pointer arithmetic is taking place # Cast Ptrs where pointer arithmetic is taking place
m[j]=resolvePtrMath(m[j],func.getPtrs()); m[j]=resolvePtrMath(m[j],func.getPtrs());
a[i] = a[i] + m[j]; a[i] = a[i] + m[j];
# special handling for some bugs in IR operations # special handling for some bugs in IR operations
line = line + re_invert.sub('\g<1> = ~\g<1>', a[i]) line = line + re_invert.sub('\g<1> = ~\g<1>', a[i])
return line; return line;
...@@ -53,7 +53,7 @@ def resolveMEM(mem,func,lhs): ...@@ -53,7 +53,7 @@ def resolveMEM(mem,func,lhs):
t = re.match('[ \w]+',mtype); t = re.match('[ \w]+',mtype);
mtype=t.group(); mtype=t.group();
if mtype.startswith('struct') and \ 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 # struct member access, try to get type from LHS
m1 = re.match(r'\*\(([\w\s]+)\*\s*\)', lhs) m1 = re.match(r'\*\(([\w\s]+)\*\s*\)', lhs)
m2 = re.match(r'\(([\w\s]+)\)', lhs) m2 = re.match(r'\(([\w\s]+)\)', lhs)
...@@ -71,31 +71,31 @@ def resolveMEM(mem,func,lhs): ...@@ -71,31 +71,31 @@ def resolveMEM(mem,func,lhs):
mem=""; mem="";
plus_sign=0; plus_sign=0;
for token in m: for token in m:
t=re_tokensplit.split(token) t=re_tokensplit.split(token)
mem_components[t[0]]=t[1]; mem_components[t[0]]=t[1];
if "symbol" in mem_components: if "symbol" in mem_components:
mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["symbol"]) mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["symbol"])
plus_sign=1; plus_sign=1;
if "base" in mem_components: if "base" in mem_components:
if plus_sign: if plus_sign:
mem=mem+" + " mem=mem+" + "
mem= mem+resolvePtrMath(mem_components["base"],func.getPtrs(),1) mem= mem+resolvePtrMath(mem_components["base"],func.getPtrs(),1)
plus_sign=1; plus_sign=1;
if "index" in mem_components: if "index" in mem_components:
if plus_sign: if plus_sign:
mem= mem+" + "; mem= mem+" + ";
mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["index"]) mem= mem+UINTPTR_CAST+re_cast.sub('', mem_components["index"])
if "step" in mem_components: if "step" in mem_components:
step=re.match('\d+',mem_components["step"]) step=re.match('\d+',mem_components["step"])
mem= mem+" * "+step.group(); mem= mem+" * "+step.group();
plus_sign=1; plus_sign=1;
if "offset" in mem_components: if "offset" in mem_components:
mem = mem + " + " mem = mem + " + "
mem_components["offset"] = mem_components["offset"].strip() mem_components["offset"] = mem_components["offset"].strip()
if mem_components["offset"].isdigit(): if mem_components["offset"].isdigit():
if int(mem_components["offset"]) >= 2**(TARGET_BITWIDTH-1): if int(mem_components["offset"]) >= 2**(TARGET_BITWIDTH-1):
mem = mem + "(int)" mem = mem + "(int)"
mem= mem+mem_components["offset"]; mem= mem+mem_components["offset"];
mem="*("+mtype+"*)("+mem+")" mem="*("+mtype+"*)("+mem+")"
return mem; return mem;
...@@ -110,8 +110,8 @@ def resolveIndirection(istr, varHash): ...@@ -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 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); m= re_1.search(istr);
if m: if m:
repl = "*"+m.group()[2:-3]+"*)("; repl = "*"+m.group()[2:-3]+"*)(";
istr=re_indirect.sub(repl,istr); istr=re_indirect.sub(repl,istr);
else: else:
m= re_2.search(istr) m= re_2.search(istr)
if m: if m:
...@@ -126,72 +126,72 @@ def resolveIndirection(istr, varHash): ...@@ -126,72 +126,72 @@ def resolveIndirection(istr, varHash):
return istr; return istr;
def removePtrCast(str): def removePtrCast(string):
re_ptrCast=re.compile('\([^\(]+ \*\)'); re_ptrCast=re.compile('\([^\(]+ \*\)');
str=re_ptrCast.sub("",str); string=re_ptrCast.sub("",string);
return str; return string;
def resolvePtrMath(str,ptrList,broad=0): def resolvePtrMath(string,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(str); m=re_fn.split(string);
str=""; string="";
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):
str=str+m[i]; string=string+m[i];
else: else:
flag=1; flag=1;
if flag: if flag:
if broad or (re.search('\S+\s+\S+',m[i])): if broad or (re.search('\S+\s+\S+',m[i])):
a=re_var.split(m[i]); a=re_var.split(m[i]);
for j in range(len(a)): for j in range(len(a)):
# constant # constant
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):
str = str + "(int)" string = string + "(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(str); strTemp=re_str.split(string);
if len(strTemp) > 1 and not strTemp[-1].strip(): if len(strTemp) > 1 and not strTemp[-1].strip():
addr = 1 addr = 1
str = ''.join(strTemp[:-2]) string = ''.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(str) strTemp=re_str.split(string)
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:
str = ''.join(strTemp[:-2]) string = ''.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:
cast = 0 cast = 0
# if it was a struct member access, don't cast # if it was a struct member 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:
cast = 0 cast = 0
# likewise if the pointer is immediately dereferenced # likewise if the pointer is immediately dereferenced
if str.strip().endswith('*'): if string.strip().endswith('*'):
cast = 0 cast = 0
# create properly casted pointer-taking operation # create properly casted pointer-taking operation
if cast: str = str+UINTPTR_CAST if cast: string = string+UINTPTR_CAST
if addr: str = str+"&" if addr: string = string+"&"
str=str+a[j]; string=string+a[j];
else: else:
str=str+m[i]; string=string+m[i];
return str; 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