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
This diff is collapsed.
This diff is collapsed.
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
** the decompressor on it. ** the decompressor on it.
*/ */
/*sds*/
#include <stdio.h> #include <stdio.h>
#include "adpcm.h" #include "adpcm.h"
#include "in_small.h" #include "in_small.h"
...@@ -29,6 +31,8 @@ ...@@ -29,6 +31,8 @@
short int pcmdata[DATASIZE]; short int pcmdata[DATASIZE];
char adpcmdata[DATASIZE/2]; char adpcmdata[DATASIZE/2];
int a[123];
struct adpcm_state coder_1_state; struct adpcm_state coder_1_state;
......
...@@ -11,35 +11,75 @@ def find(f, seq): ...@@ -11,35 +11,75 @@ def find(f, seq):
for item in seq: for item in seq:
if f(item): if f(item):
return item return item
class GlobalVariable: class Variable:
def __init__(self): def __init__(self):
self.isLocal = False
self.name = "" self.name = ""
self.address = 0
self.type = "" self.type = ""
self.length = -1 self.length = -1
self.size = -1 self.size = -1
self.file = ""
self.lineNum = -1
def __init__(self, name, type, length, file):
self.name = name
self.address = -1 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.type = type
self.length = length self.length = length
self.file = file self.scope = scope
self.lineNum = -1 self.address = address
self.size = size
def setAddress(self, address): def setAddress(self, address):
self.address = address self.address = address
def setSize(self, size): def setSize(self, size):
self.size = size self.size = size
def debug(self): def debug(self):
print ("%s\t\t0x%x\t\t(type=%s; size=%d) - %s" % if self.isLocal == True:
(self.name, self.address, self.type, self.size, print ("LocalVar: "),
self.file)) 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): def debugListGlobalVariables(listGlobalVariables):
...@@ -100,10 +140,11 @@ def getGlobalVariablesInfoFromGDB(listBinaryFileNames): ...@@ -100,10 +140,11 @@ def getGlobalVariablesInfoFromGDB(listBinaryFileNames):
varLen = int(m.group(3)) varLen = int(m.group(3))
else: else:
varLen = 1 varLen = 1
currListGlobalVariables.append(GlobalVariable(name=varName, currListGlobalVariables.append(Variable(isLocal = False,
type=dataType, name=varName,
length=varLen, type=dataType,
file=currFileName)) length=varLen,
scope=currFileName))
gdbOFile.close() gdbOFile.close()
# Fetch addresses for Global Variables in this file # Fetch addresses for Global Variables in this file
...@@ -145,26 +186,26 @@ def getGlobalVariablesInfoFromGDB(listBinaryFileNames): ...@@ -145,26 +186,26 @@ def getGlobalVariablesInfoFromGDB(listBinaryFileNames):
listGlobalVariables = listGlobalVariables + currListGlobalVariables listGlobalVariables = listGlobalVariables + currListGlobalVariables
debugListGlobalVariables(listGlobalVariables) debugListVariables(listGlobalVariables)
return listGlobalVariables return listGlobalVariables
class LocalVariable: # class LocalVariable:
def __init__(self, name, address, type, size, funcName): # def __init__(self, name, address, type, size, funcName):
self.name = name # self.name = name
self.type = type # self.type = type
self.address = address # self.address = address
self.funcName = funcName # self.funcName = funcName
self.size = size # self.size = size
#
def debug(self): # def debug(self):
logging.debug("LocalVar %s in func %s, address = %d, type = \"%s\", size = %d" % # logging.debug("LocalVar %s in func %s, address = %d, type = \"%s\", size = %d" %
(self.name, self.funcName, self.address, self.type, self.size)) # (self.name, self.funcName, self.address, self.type, self.size))
#
def debugPrintListLocalVariables(listLocalVariables): # def debugPrintListLocalVariables(listLocalVariables):
for var in listLocalVariables: # for var in listLocalVariables:
var.debug() # var.debug()
print "" # print ""
def getLocalVariablesForAllFunc(listBinaryFileNames, listFunctionsObj): def getLocalVariablesForAllFunc(listBinaryFileNames, listFunctionsObj):
binaryFileName = listBinaryFileNames[0] binaryFileName = listBinaryFileNames[0]
...@@ -287,14 +328,18 @@ def getLocalVariablesForAllFunc(listBinaryFileNames, listFunctionsObj): ...@@ -287,14 +328,18 @@ def getLocalVariablesForAllFunc(listBinaryFileNames, listFunctionsObj):
m = re_sizeLine.match(line) m = re_sizeLine.match(line)
if m is not None: if m is not None:
varSize = int(m.group("varSize")) varSize = int(m.group("varSize"))
listLocalVariables.append(LocalVariable(varName, address, listLocalVariables.append(Variable(isLocal = True,
varType, name = varName,
varSize, type = varType,
func.functionName)) length = -1,
scope = func.functionName,
address = address,
size = varSize))
continue continue
gdbOFile.close() gdbOFile.close()
debugPrintListLocalVariables(listLocalVariables) debugListVariables(listLocalVariables)
return listLocalVariables return listLocalVariables
def sizeOf(type): 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