Commit bafc2b5a authored by Gaurav Kukreja's avatar Gaurav Kukreja

Incomplete code

Signed-off-by: Gaurav Kukreja's avatarGaurav Kukreja <gaurav@gauravk.in>
parent 44d77cc0
......@@ -238,25 +238,25 @@ expression << ( assignment_expression
constant_expression << conditional_expression
declaration << ( (declaration_specifiers + ';')
| (declaration_specifiers + init_declarator_list + ';')
)
declaration_specifiers << ( storage_class_specifier
| (storage_class_specifier + declaration_specifiers)
| type_specifier
| (type_specifier + declaration_specifiers)
| type_qualifier
| (type_qualifier + declaration_specifiers)
)
init_declarator_list << ( init_declarator
| (init_declarator_list + ',' + init_declarator)
)
init_declarator << ( declarator
| (declarator + '=' + initializer)
)
# declaration << ( (declaration_specifiers + ';')
# | (declaration_specifiers + init_declarator_list + ';')
# )
#
# declaration_specifiers << ( storage_class_specifier
# | (storage_class_specifier + declaration_specifiers)
# | type_specifier
# | (type_specifier + declaration_specifiers)
# | type_qualifier
# | (type_qualifier + declaration_specifiers)
# )
#
# init_declarator_list << ( init_declarator
# | (init_declarator_list + ',' + init_declarator)
# )
#
# init_declarator << ( declarator
# | (declarator + '=' + initializer)
# )
storage_class_specifier << ( TYPEDEF
| EXTERN
......@@ -288,11 +288,11 @@ struct_or_union << ( STRUCT
| UNION
)
struct_declaration_list << ( struct_declaration
| (struct_declaration_list + struct_declaration)
)
struct_declaration << specifier_qualifier_list + struct_declarator_list + ';'
# struct_declaration_list << ( struct_declaration
# | (struct_declaration_list + struct_declaration)
# )
#
# struct_declaration << specifier_qualifier_list + struct_declarator_list + ';'
specifier_qualifier_list << ( (type_specifier + specifier_qualifier_list)
| type_specifier
......@@ -300,44 +300,44 @@ specifier_qualifier_list << ( (type_specifier + specifier_qualifier_list)
| (type_qualifier)
)
struct_declarator_list << ( struct_declarator
| (struct_declarator_list + ',' + struct_declarator)
)
struct_declarator << ( declarator
| (':' + constant_expression)
| (declarator + ':' + constant_expression)
)
enum_specifier << ( (ENUM + '{' + enumerator_list + '}')
| (ENUM + IDENTIFIER + '{' + enumerator_list + '}')
| (ENUM + IDENTIFIER)
)
enumerator_list << ( enumerator
| (enumerator_list + ',' + enumerator)
)
enumerator << ( IDENTIFIER
| (IDENTIFIER + '=' + constant_expression)
)
# struct_declarator_list << ( struct_declarator
# | (struct_declarator_list + ',' + struct_declarator)
# )
#
# struct_declarator << ( declarator
# | (':' + constant_expression)
# | (declarator + ':' + constant_expression)
# )
#
# enum_specifier << ( (ENUM + '{' + enumerator_list + '}')
# | (ENUM + IDENTIFIER + '{' + enumerator_list + '}')
# | (ENUM + IDENTIFIER)
# )
#
# enumerator_list << ( enumerator
# | (enumerator_list + ',' + enumerator)
# )
#
# enumerator << ( IDENTIFIER
# | (IDENTIFIER + '=' + constant_expression)
# )
type_qualifier << ( CONST
| VOLATILE
)
declarator << ( pointer + direct_declarator
| direct_declarator
)
direct_declarator << ( IDENTIFIER
| ('(' + declarator + ')')
| (direct_declarator + '[' + constant_expression + ']')
| (direct_declarator + '[' + ']')
| (direct_declarator + '(' + parameter_type_list + ')')
| (direct_declarator + '(' + identifier_list + ')')
| (direct_declarator + '(' + ')')
)
# declarator << ( pointer + direct_declarator
# | direct_declarator
# )
#
# direct_declarator << ( IDENTIFIER
# | ('(' + declarator + ')')
# | (direct_declarator + '[' + constant_expression + ']')
# | (direct_declarator + '[' + ']')
# | (direct_declarator + '(' + parameter_type_list + ')')
# | (direct_declarator + '(' + identifier_list + ')')
# | (direct_declarator + '(' + ')')
# )
pointer << ( '*'
| ('*' + type_qualifier_list)
......@@ -349,114 +349,114 @@ type_qualifier_list << ( type_qualifier
| (type_qualifier_list + type_qualifier)
)
parameter_type_list << ( parameter_list
| (parameter_list + ',' + ELLIPSIS)
)
parameter_list << ( parameter_declaration
| (parameter_list + ',' + parameter_declaration)
)
parameter_declaration << ( (declaration_specifiers + declarator)
| (declaration_specifiers + abstract_declarator)
| (declaration_specifiers)
)
identifier_list << ( IDENTIFIER
| (identifier_list + ',' + IDENTIFIER)
)
# parameter_type_list << ( parameter_list
# | (parameter_list + ',' + ELLIPSIS)
# )
#
# parameter_list << ( parameter_declaration
# | (parameter_list + ',' + parameter_declaration)
# )
#
# parameter_declaration << ( (declaration_specifiers + declarator)
# | (declaration_specifiers + abstract_declarator)
# | (declaration_specifiers)
# )
# identifier_list << ( IDENTIFIER
# | (identifier_list + ',' + IDENTIFIER)
# )
type_name << ( specifier_qualifier_list
| (specifier_qualifier_list + abstract_declarator)
# | (specifier_qualifier_list + abstract_declarator)
)
abstract_declarator << ( pointer
| direct_abstract_declarator
| (pointer + direct_abstract_declarator)
)
direct_abstract_declarator << ( ('(' + abstract_declarator + ')')
| ('[' + ']')
| ('[' + constant_expression + ']')
| (direct_abstract_declarator + '[' + ']')
| (direct_abstract_declarator + '[' + constant_expression + ']')
| ('(' + ')')
| ('(' + parameter_type_list + ')')
| (direct_abstract_declarator + '(' + ')')
| (direct_abstract_declarator + '(' + parameter_type_list + ')')
)
initializer << ( assignment_expression
| ('{' + initializer_list + '}')
| ('{' + initializer_list + ',' + '}')
)
initializer_list << ( initializer
| (initializer_list + ',' + initializer)
)
statement << ( labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
)
labeled_statement << ( (IDENTIFIER + ':' + statement)
| (CASE + constant_expression + ':' + statement)
| (DEFAULT + ':' + statement)
)
compound_statement << ( ('{' + '}')
| ('{' + statement_list + '}')
| ('{' + declaration_list + '}')
| ('{' + declaration_list + statement_list + '}')
)
declaration_list << ( declaration
| (declaration_list + declaration)
)
statement_list << ( statement
| (statement_list + statement)
)
# abstract_declarator << ( pointer
# | direct_abstract_declarator
# | (pointer + direct_abstract_declarator)
# )
#
# direct_abstract_declarator << ( ('(' + abstract_declarator + ')')
# | ('[' + ']')
# | ('[' + constant_expression + ']')
# | (direct_abstract_declarator + '[' + ']')
# | (direct_abstract_declarator + '[' + constant_expression + ']')
# | ('(' + ')')
# | ('(' + parameter_type_list + ')')
# | (direct_abstract_declarator + '(' + ')')
# | (direct_abstract_declarator + '(' + parameter_type_list + ')')
# )
# initializer << ( assignment_expression
# | ('{' + initializer_list + '}')
# | ('{' + initializer_list + ',' + '}')
# )
#
# initializer_list << ( initializer
# | (initializer_list + ',' + initializer)
# )
#
# statement << ( labeled_statement
# | compound_statement
# | expression_statement
# | selection_statement
# | iteration_statement
# | jump_statement
# )
#
# labeled_statement << ( (IDENTIFIER + ':' + statement)
# | (CASE + constant_expression + ':' + statement)
# | (DEFAULT + ':' + statement)
# )
#
# compound_statement << ( ('{' + '}')
# | ('{' + statement_list + '}')
# | ('{' + declaration_list + '}')
# | ('{' + declaration_list + statement_list + '}')
# )
#
# declaration_list << ( declaration
# | (declaration_list + declaration)
# )
#
# statement_list << ( statement
# | (statement_list + statement)
# )
expression_statement << ( ';'
| (expression + ';')
)
selection_statement << ( (IF + '(' + expression + ')' + statement)
| (IF + '(' + expression + ')' + statement + ELSE + statement)
| (SWITCH + '(' + expression + ')' + statement)
)
iteration_statement << ( (WHILE + '(' + expression + ')' + statement)
| (DO + statement + WHILE + '(' + expression + ')' + ';')
| (FOR + '(' + expression_statement + expression_statement + ')' + statement)
| (FOR + '(' + expression_statement + expression_statement + expression + ')' + statement)
)
jump_statement << ( (GOTO + IDENTIFIER + ';')
| (CONTINUE + ';')
| (BREAK + ';')
| (RETURN + ';')
| (RETURN + expression + ';')
)
translation_unit << ( external_declaration
| (translation_unit + external_declaration)
)
external_declaration << ( function_definition
| declaration
)
function_definition << ( (declaration_specifiers + declarator + declaration_list + compound_statement)
| (declaration_specifiers + declarator + compound_statement)
| (declarator + declaration_list + compound_statement)
| (declarator + compound_statement)
)
# selection_statement << ( (IF + '(' + expression + ')' + statement)
# | (IF + '(' + expression + ')' + statement + ELSE + statement)
# | (SWITCH + '(' + expression + ')' + statement)
# )
#
# iteration_statement << ( (WHILE + '(' + expression + ')' + statement)
# | (DO + statement + WHILE + '(' + expression + ')' + ';')
# | (FOR + '(' + expression_statement + expression_statement + ')' + statement)
# | (FOR + '(' + expression_statement + expression_statement + expression + ')' + statement)
# )
#
# jump_statement << ( (GOTO + IDENTIFIER + ';')
# | (CONTINUE + ';')
# | (BREAK + ';')
# | (RETURN + ';')
# | (RETURN + expression + ';')
# )
#
# translation_unit << ( external_declaration
# | (translation_unit + external_declaration)
# )
#
# external_declaration << ( function_definition
# | declaration
# )
#
# function_definition << ( (declaration_specifiers + declarator + declaration_list + compound_statement)
# | (declaration_specifiers + declarator + compound_statement)
# | (declarator + declaration_list + compound_statement)
# | (declarator + compound_statement)
# )
if __name__ == "__main__":
str = "a = b"
......
......@@ -48,13 +48,15 @@ def getGlobalVariableAddressTableForFunc(funcObj):
def findLocalVar(funcName, addRelSP, listLocalVariables):
for var in listLocalVariables:
if var.funcName == funcName:
assert (var.isLocal == True)
if var.scope == funcName:
if var.address <= addRelSP and (var.address + var.size) > addRelSP :
return var
return None
def findGlobalVar(address, listGlobalVariables):
for var in listGlobalVariables:
assert (var.isLocal == False)
if var.address <= address and (var.address + var.size) > address:
return var
return None
......@@ -64,22 +66,123 @@ class FunctionInitState:
self.name = name
self.initRegState = initRegState
class LoadStoreInfo:
def __init__(self):
self.isLoad = False
self.var = None
self.isPCRelLoad = False
self.PCRelAdd = -1
self.isSpiltRegister = False
self.spiltRegAdd = -1
self.isAccuratelyMatched = False
self.lineNumObj = -1
def loadVar(self, var, lineNum):
self.isLoad = True
self.var = var
self.isAccuratelyMatched = True
self.lineNumObj = lineNum
def storeVar(self, var, lineNum):
self.isLoad = False
self.var = var
self.isAccuratelyMatched = True
self.lineNumObj = lineNum
def loadInaccurate(self, lineNum):
self.isLoad = True
self.var = None
self.lineNumObj = lineNum
self.isAccuratelyMatched = False
def storeInaccurate(self, lineNum):
self.isLoad = False
self.var = None
self.lineNumObj = lineNum
self.isAccuratelyMatched = False
def loadPCRel(self, PCRelAdd, lineNum):
self.isLoad = True
self.isPCRelLoad = True
self.PCRelAdd = PCRelAdd
self.lineNumObj = lineNum
self.isAccuratelyMatched = True
def spillReg(self, spiltRegAdd, lineNum):
self.isLoad = False
self.isSpiltRegister = True
self.spiltRegAdd = spiltRegAdd
self.lineNumObj = lineNum
self.isAccuratelyMatched = True
def readSpiltReg(self, spiltRegAdd, lineNum):
self.isLoad = True
self.isSpiltRegister = True
self.spiltRegAdd = spiltRegAdd
self.lineNumObj = lineNum
self.isAccuratelyMatched = True
def debug(self):
if self.isAccuratelyMatched == True:
if self.isLoad == True:
if self.isPCRelLoad == False:
if self.isSpiltRegister == False:
print("Load "),
if self.var.isLocal == True:
print("LocalVar "),
else:
print("GlobalVar "),
print("%s at line %d" % (self.var.name, self.lineNumObj))
else: # isSpiltRegister == True
print("Reading Spilt Register from add %d in stack on line %d" %
(self.spiltRegAdd, self.lineNumObj))
else: # isPCRelLoad == True
print("PC Relative Load from add %d on line %d" %
(self.PCRelAdd, self.lineNumObj))
else: # isLoad == False
if self.isSpiltRegister == False:
print("Store "),
if self.var.isLocal == True:
print("LocalVar "),
else:
print("GlobalVar "),
print("%s at line %d" % (self.var.name, self.lineNumObj))
else: # isSpiltRegister == True
print ("Spilling Register to add %d in stack on line %d" %
(self.spiltRegAdd, self.lineNumObj))
else:
if self.isLoad == True:
print("Inaccurately Matched Load at line %d" % self.lineNumObj)
else:
print("Inaccurately Matched Store at line %d" % self.lineNumObj)
def debugListLSInfo(listLSInfo):
print ""
for lsInfo in listLSInfo:
lsInfo.debug()
print ""
listGlobalVariables = []
listLocalVariables = []
listISCFunctions = []
listObjdumpFunctions = []
# TODO: Probably need to change the name of this function
def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames):
(listISCFunctions, listObjdumpFunctions) = match_cfg(listISCFileNames,
listObjdumpFileNames,
listBinaryFileNames)
listGlobalVariables = getGlobalVariablesInfoFromGDB(listBinaryFileNames)
listLocalVariables = getLocalVariablesForAllFunc(listBinaryFileNames, listObjdumpFunctions)
def identifyLoadStore():
global listISCFunctions
global listObjdumpFunctions
global listGlobalVariables
global listLocalVariables
listEmulatedFunctions = []
queuePendingFunction = deque([FunctionInitState("main")])
currFuncSpilledRegister = {}
listLSInfo = []
while queuePendingFunction:
func = queuePendingFunction.popleft()
logging.debug("")
......@@ -92,6 +195,7 @@ def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames)
dictGlobVarAddAtTableAddress = getGlobalVariableAddressTableForFunc(funcObj)
armEmu = ArmEmulator(dictGlobVarAddAtTableAddress, func.initRegState)
currFuncListLSInfo = []
for lineNumObj in range(funcObj.startLine, funcObj.endLine + 1):
lineObj = lc.getline(funcObj.fileName, lineNumObj)
......@@ -171,10 +275,18 @@ def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames)
strAdd - armEmu.reg["sp"].value,
listLocalVariables)
if localVar is not None:
lsInfo = LoadStoreInfo()
lsInfo.storeVar(localVar, lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
logging.debug(" %d: Writing local var %s" %
(lineNumObj, localVar.name))
else:
destRegVal = armEmu.reg[destReg].value
lsInfo = LoadStoreInfo()
lsInfo.spillReg((strAdd - armEmu.reg["sp"].value), lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
currFuncSpilledRegister[strAdd - armEmu.reg["sp"].value] = destRegVal
logging.debug(" %d: Spilling Register %s to address %d ( = %d)" %
(lineNumObj, destReg,
......@@ -185,10 +297,18 @@ def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames)
globalVar = findGlobalVar(strAdd,
listGlobalVariables)
if globalVar is not None:
lsInfo = LoadStoreInfo()
lsInfo.storeVar(globalVar, lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
logging.debug(" %d: Writing Global Var %s" %
(lineNumObj, globalVar.name))
continue
else:
lsInfo = LoadStoreInfo()
lsInfo.storeInaccurate(lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
logging.error(" %d: %s" % (lineNumObj, instObj))
continue
......@@ -228,6 +348,10 @@ def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames)
assert(m_comment)
addInTable = int(m_comment.group(1), 16)
assert(addInTable in dictGlobVarAddAtTableAddress)
lsInfo = LoadStoreInfo()
lsInfo.loadPCRel(addInTable, lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
address = dictGlobVarAddAtTableAddress[addInTable]
globalVar = find(lambda var: var.address == address,
listGlobalVariables)
......@@ -253,9 +377,17 @@ def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames)
ldrAdd - armEmu.reg["sp"].value,
listLocalVariables)
if localVar is not None:
lsInfo = LoadStoreInfo()
lsInfo.loadVar(localVar, lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
logging.debug(" %d: Reading local var %s" %
(lineNumObj, localVar.name))
else:
lsInfo = LoadStoreInfo()
lsInfo.readSpiltReg((ldrAdd - armEmu.reg["sp"].value), lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
armEmu.reg[destReg].setValue(currFuncSpilledRegister[ldrAdd - armEmu.reg["sp"].value])
logging.debug(" %d: Reading Spilled Register in %s from address %d ( = %d)" %
(lineNumObj, destReg,
......@@ -266,18 +398,72 @@ def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames)
globalVar = findGlobalVar(ldrAdd,
listGlobalVariables)
if globalVar is not None:
lsInfo = LoadStoreInfo()
lsInfo.loadVar(globalVar, lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
logging.debug(" %d: Reading Global Var %s" %
(lineNumObj, globalVar.name))
continue
else:
lsInfo = LoadStoreInfo()
lsInfo.loadInaccurate(lineNumObj)
currFuncListLSInfo.append(lsInfo)
del lsInfo
logging.error(" %d: %s" % (lineNumObj, instObj))
continue
else:
logging.error(" %d: Instruction does not match!")
return -1
debugListLSInfo(currFuncListLSInfo)
listLSInfo = listLSInfo + currFuncListLSInfo
return listLSInfo
def findLoadStoreBetweenLines(listLSInfo, startLine, endLine):
listLSInfoInBlock = []
for lsInfo in listLSInfo:
if lsInfo.lineNumObj in range(startLine, endLine+1):
listLSInfoInBlock.append(lsInfo)
return listLSInfoInBlock
def instrumentCache(listISCFileNames, listObjdumpFileNames, listBinaryFileNames):
global listISCFunctions
global listObjdumpFunctions
global listGlobalVariables
global listLocalVariables
global listLSInfo
(listISCFunctions, listObjdumpFunctions) = match_cfg(listISCFileNames,
listObjdumpFileNames,
listBinaryFileNames)
listGlobalVariables = getGlobalVariablesInfoFromGDB(listBinaryFileNames)
listLocalVariables = getLocalVariablesForAllFunc(listBinaryFileNames, listObjdumpFunctions)
listLSInfo = identifyLoadStore()
for funcObj in listObjdumpFunctions:
fileNameObj = funcObj.fileName
funcISC = find(lambda fn: fn.functionName == funcObj.name,
listISCFunctions)
fileNameISC = funcISC.fileName
for blockObj in funcObj.listBlocks:
listLSInfoInBlock = findLoadStoreBetweenLines(listLSInfo,
blockObj.startLine,
blockObj.endLine)
for lsInfo in listLSInfoInBlock:
for blockIndISC in blockObj.mapsTo:
blockISC = funcISC.listBlocks[blockIndISC]
for lineNumISC in range(blockISC.startLine, blockISC.endLine+1):
lineISC = lc.getline(fileNameISC, lineNumISC)
if __name__ == "__main__":
# listISCFileNames = []
......
......@@ -16,6 +16,8 @@
** the decompressor on it.
*/
/*sds*/
#include <stdio.h>
#include "adpcm.h"
#include "in_small.h"
......@@ -29,6 +31,8 @@
short int pcmdata[DATASIZE];
char adpcmdata[DATASIZE/2];
int a[123];
struct adpcm_state coder_1_state;
......
......@@ -11,35 +11,75 @@ def find(f, seq):
for item in seq:
if f(item):
return item
class GlobalVariable:
class Variable:
def __init__(self):
self.isLocal = False
self.name = ""
self.address = 0
self.type = ""
self.length = -1
self.size = -1
self.file = ""
self.lineNum = -1
def __init__(self, name, type, length, file):
self.name = name
self.address = -1
self.scope = ""
def __init__(self, isLocal, name, type, length, scope, address = -1, size = -1):
self.isLocal = isLocal
self.name = name
self.type = type
self.length = length
self.file = file
self.lineNum = -1
self.scope = scope
self.address = address
self.size = size
def setAddress(self, address):
self.address = address
def setSize(self, size):
self.size = size
def debug(self):
print ("%s\t\t0x%x\t\t(type=%s; size=%d) - %s" %
(self.name, self.address, self.type, self.size,
self.file))
if self.isLocal == True:
print ("LocalVar: "),
else:
print("GlobalVar: "),
print("%s; add=0x%x; scope=\"%s\"; type=\"%s\"; size=%d;" %
(self.name, self.address, self.scope, self.type, self.size))
def debugListVariables(listVariables):
print ""
for var in listVariables:
var.debug()
print ""
# class GlobalVariable:
# def __init__(self):
# self.name = ""
# self.address = 0
# self.type = ""
# self.length = -1
# self.size = -1
# self.file = ""
# self.lineNum = -1
#
# def __init__(self, name, type, length, file):
# self.name = name
# self.address = -1
# self.type = type
# self.length = length
# self.file = file
# self.lineNum = -1
#
# def setAddress(self, address):
# self.address = address
#
# def setSize(self, size):
# self.size = size
#
# def debug(self):
# print ("%s\t\t0x%x\t\t(type=%s; size=%d) - %s" %
# (self.name, self.address, self.type, self.size,
# self.file))
def debugListGlobalVariables(listGlobalVariables):
......@@ -100,10 +140,11 @@ def getGlobalVariablesInfoFromGDB(listBinaryFileNames):
varLen = int(m.group(3))
else:
varLen = 1
currListGlobalVariables.append(GlobalVariable(name=varName,
type=dataType,
length=varLen,
file=currFileName))
currListGlobalVariables.append(Variable(isLocal = False,
name=varName,
type=dataType,
length=varLen,
scope=currFileName))
gdbOFile.close()
# Fetch addresses for Global Variables in this file
......@@ -145,26 +186,26 @@ def getGlobalVariablesInfoFromGDB(listBinaryFileNames):
listGlobalVariables = listGlobalVariables + currListGlobalVariables
debugListGlobalVariables(listGlobalVariables)
debugListVariables(listGlobalVariables)
return listGlobalVariables
class LocalVariable:
def __init__(self, name, address, type, size, funcName):
self.name = name
self.type = type
self.address = address
self.funcName = funcName
self.size = size
def debug(self):
logging.debug("LocalVar %s in func %s, address = %d, type = \"%s\", size = %d" %
(self.name, self.funcName, self.address, self.type, self.size))
def debugPrintListLocalVariables(listLocalVariables):
for var in listLocalVariables:
var.debug()
print ""
# class LocalVariable:
# def __init__(self, name, address, type, size, funcName):
# self.name = name
# self.type = type
# self.address = address
# self.funcName = funcName
# self.size = size
#
# def debug(self):
# logging.debug("LocalVar %s in func %s, address = %d, type = \"%s\", size = %d" %
# (self.name, self.funcName, self.address, self.type, self.size))
#
# def debugPrintListLocalVariables(listLocalVariables):
# for var in listLocalVariables:
# var.debug()
# print ""
def getLocalVariablesForAllFunc(listBinaryFileNames, listFunctionsObj):
binaryFileName = listBinaryFileNames[0]
......@@ -287,14 +328,18 @@ def getLocalVariablesForAllFunc(listBinaryFileNames, listFunctionsObj):
m = re_sizeLine.match(line)
if m is not None:
varSize = int(m.group("varSize"))
listLocalVariables.append(LocalVariable(varName, address,
varType,
varSize,
func.functionName))
listLocalVariables.append(Variable(isLocal = True,
name = varName,
type = varType,
length = -1,
scope = func.functionName,
address = address,
size = varSize))
continue
gdbOFile.close()
debugPrintListLocalVariables(listLocalVariables)
debugListVariables(listLocalVariables)
return listLocalVariables
def sizeOf(type):
......
from pyparsing import *
Ident = Word(alphas, alphanums+'_')
Num = Word(nums+'.')
String = quotedString
MUL_ASSIGN = "*"
DIV_ASSIGN = "/"
MOD_ASSIGN = "%"
ADD_ASSIGN = "+"
SUB_ASSIGN = "-"
LEFT_ASSIGN = "<<="
RIGHT_ASSIGN = ">>="
AND_ASSIGN = "&="
XOR_ASSIGN ="^="
OR_ASSIGN = "|="
IncOp = "++"
DecOp = "--"
LPAREN = Suppress("(")
RPAREN = Suppress(")")
UnaryOp = ( "&" |
"*" |
"+" |
"-" |
"~" |
"!"
)
Sizeof = "sizeof"
StructUnion = ( "struct" |
"union" )
StructDeclList = Forward()
StructDeclList << ( (StructDecl) |
(StructDeclList + StructDecl)
)
StructDecl = ( SpecifierQualifierList + StructDeclaratorList + ';' )
StructDeclaratorList = Forward()
StructUnionSpecifier = ( (StructUnion + Ident + '{' + StructDeclList + '}') |
(StructUnion + '{' + StructDeclList + '}') |
(StructUnion + Ident)
)
TypeSpecifier = ( "void" |
"char" |
"short" |
"int" |
"long" |
"float" |
"double" |
"signed" |
"unsigned" |
StructUnionSpecifier |
EnumSpecifier |
TypeName
)
SpecifierQualifierList = Forward()
SpecifierQualifierList << ( TypeSpecifier + SpecifierQualifierList |
TypeSpecifier |
TypeQualifier + SpecifierQualifierList |
TypeQualifier
)
TypeName = ( SpecifierQualifierList |
(SpecifierQualifierList + AbstractDeclarator)
)
CastExpression = ( UnaryExpression |
(LPAREN + TypeName + RPAREN + CastExpression)
)
UnaryExpression = Forward()
UnaryExpression << (PostExpression |
(IncOp + UnaryExpression) |
(DecOp + UnaryExpression) |
(UnaryOp + CastExpresssion) |
(Sizeof + UnaryExpression) |
(Sizeof + LPAREN + TypeName + RParen)
)
AssignOperator = ( '='
| MUL_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| ADD_ASSIGN
| SUB_ASSIGN
| LEFT_ASSIGN
| RIGHT_ASSIGN
| AND_ASSIGN
| XOR_ASSIGN
| OR_ASSIGN
)
AssignExpression = Forward()
AssignExpression << ( CondExpression |
(UnaryExpression + AssignOperator + AssignExpression)
)
Expression = Forward()
Expression << ( AssignExpression |
(Expression + ',' + AssignExpression)
)
PrimExpression = ( Ident |
Num |
String |
(LPAREN + Expression + RPAREN )
)
PostExpression = Forward()
PostExpression << ( PrimExpression |
)
if __name__ == "__main__":
str_varDecl = "unsigned short int a;"
\ No newline at end of file
import re
import logging
# Comments
re_CommentStart = re.compile("\s*/\*.*")
re_CommentEnd = re.compile(".*\*/")
CommentOneLine = "\s*//.*"
CommentLine = "\s*/\*.*\*/"
re_Comment = re.compile("(?:(?:%s)|(?:%s))" % (CommentOneLine, CommentLine))
# Pre-processor directives
PreProcDir = "\s*#(?:include|define|ifndef|endif).*"
re_PreProcDir = re.compile("(?:%s)" % PreProcDir)
# Variable Declarations
TypeSpecifiers = "char|int|float|double"
OptionalSpecifiers = "signed|unsigned|short|long"
StorageClass = "(?:static|extern|auto|register)"
UserDefined = "struct\s*[\w_]*"
IRCSpecific = "uintptr_t"
DataTypes = "(?:%s)?\s*(?:%s\s*)*\s*(?:(?:%s)|(?:%s)|(?:%s))?\s*(?:\*)*" % (StorageClass, OptionalSpecifiers, TypeSpecifiers, UserDefined, IRCSpecific)
VarSpec = "(?P<varType>%s)\s*(?P<varName>\w*)\s*(?P<varLen>(?:\[.*\])*)?" % (DataTypes)
re_VarDecl = re.compile("\s*(?:%s);" % (VarSpec))
# Function Definition Start
VarSpec_ = "(?:%s)\s*(?:\w*)\s*(?:(?:\[.*\])*)?" % (DataTypes)
FuncParams = "(?:(?:%s)(?:\s*,\s*(?:%s))*)" % (VarSpec_, VarSpec_)
RetTypes = "(?:%s)|void" % (DataTypes)
re_FuncDefStart = re.compile("\s*(?P<retType>%s)?\s*(?P<name>\w*)\s*\((?P<params>%s)?\)\s*(?P<openBrace>\{)?" % (RetTypes, FuncParams))
# Label
re_Label = re.compile("\s*(?P<label>\w*):")
# Assignment/Arithmetic
Var = "(?:\s*\w*\s*)"
Const = "(?:\s*\d*\s*)"
Oper = "(?:\+|\-|\*|/)"
re_Assign = re.compile("\s*.*\s*=\s*.*;")
inMultiLineComment = False
def inComment(line, lineNum):
global inMultiLineComment
if inMultiLineComment == True:
m = re_CommentEnd.match(line)
if m is not None:
inMultiLineComment = False
# logging.debug("%d: Comment end" % lineNum)
return True
m = re_Comment.match(line)
if m is not None:
# logging.debug("%d: One Line Comment" % lineNum)
return True
m = re_CommentStart.match(line)
if m is not None:
inMultiLineComment = True
# logging.debug("%d: Comment start" % lineNum)
return True
else:
return False
def isPreProcDir(line, lineNum):
m = re_PreProcDir.match(line)
if m is not None:
# logging.debug("%d: Preprocessor Directive" % lineNum)
return True
else:
return False
def shouldIgnore(line, lineNum):
if inComment(line, lineNum):
return True
elif isPreProcDir(line, lineNum):
return True
elif line == "":
# logging.debug("%d: Empty Line" % lineNum)
return True
return False
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
# fileName = "examples/adpcm/my_ctop_IR.c"
fileName = "examples/adpcm/adpcm_IR.c"
file = open(fileName, "r")
lineNum = 0
for line in file:
lineNum = lineNum + 1
if shouldIgnore(line, lineNum):
continue
m = re_VarDecl.match(line)
if m is not None:
varName = m.group("varName")
logging.debug("%d: VarDecl: \"%s\"" % (lineNum, varName))
continue
m = re_FuncDefStart.match(line)
if m is not None:
funcName = m.group("name")
logging.debug("%d: FuncDef: \"%s\"" % (lineNum, funcName))
continue
m = re_Label.match(line)
if m is not None:
label = m.group("label")
logging.debug("%d: Label: \"%s\"" % (lineNum, label))
continue
m =
continue
\ No newline at end of file
import sys
if __name__ == "__main__":
fileName = "./abc.txt"
file = open(fileName, "w")
str = "Hello\nWorld!"
file.write(str)
file.close()
\ No newline at end of file
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