Commit 4887d42d authored by Gaurav Kukreja's avatar Gaurav Kukreja

DON'T TRUST: Removed regex from cfg_isc.py to use from irc_regex.py

Signed-off-by: Gaurav Kukreja's avatarGaurav Kukreja <gaurav@gauravk.in>
parent ec2dc851
...@@ -5,22 +5,7 @@ ...@@ -5,22 +5,7 @@
import sys import sys
import re import re
from cfg import * from cfg import *
from irc_regex import *
# RE for function definition
# group(1) is function Name
# if group(2) == "){", single line definition
# elif group(2) == $, multi line definition
re_funcDef = re.compile('(?:\w*\s+)*\**(\w+)\s*\([,\s\w&\[\]\*]*($|\)\s*\{)')
re_funcDefArgLine = re.compile('[,\s\w&\[\]\*]*($|\)\s*(?:\{)?)')
re_basicBlockLabel = re.compile('\s*(\w*bb_[0-9]*):')
re_basicBlockStart = re.compile('\s*//\s*#\s*PRED:./*')
re_basicBlockEnd = re.compile('\s*//\s*#\s*SUCC:./*')
re_gotoLine = re.compile('\s*goto\s*(\w*bb_[0-9]*);')
re_funcCallLine = re.compile('\s*(\w*)\s*\([,\s\w&\[\]\*]*\);')
re_returnLine = re.compile('\s*return\s*.*;')
re_funcDefEnd = re.compile('\s*\}')
re_commentStart = re.compile('\s*/\*.*')
re_commentEnd = re.compile('\s*.*\*/')
class BasicBlockTargets: class BasicBlockTargets:
def __init__(self, name, listTargets = None): def __init__(self, name, listTargets = None):
...@@ -80,16 +65,16 @@ def parse_isc(fileName): ...@@ -80,16 +65,16 @@ def parse_isc(fileName):
''' '''
# Comment Handling # Comment Handling
m = re_commentStart.match(line) m = re_CommentStart.match(line)
if m is not None: if m is not None:
if re_commentEnd.match(line) == None: if re_CommentEnd.match(line) == None:
inMultiLineComment = 1 inMultiLineComment = 1
continue continue
else: else:
continue continue
if inMultiLineComment == 1: if inMultiLineComment == 1:
m = re_commentEnd.match(line) m = re_CommentEnd.match(line)
if m is not None: if m is not None:
inMultiLineComment = 0 inMultiLineComment = 0
continue continue
...@@ -97,9 +82,9 @@ def parse_isc(fileName): ...@@ -97,9 +82,9 @@ def parse_isc(fileName):
continue continue
# 1. Look for function definition # 1. Look for function definition
m = re_funcDef.match(line) m = re_FuncDefStart.match(line)
if m is not None: if m is not None:
if m.group(2) == "": if m.group("openBrace") == "":
# 1.b. Multi Line Function Arguments # 1.b. Multi Line Function Arguments
inFuncDefArgMultiLine = 1 inFuncDefArgMultiLine = 1
currFuncName = m.group(1) currFuncName = m.group(1)
...@@ -107,15 +92,15 @@ def parse_isc(fileName): ...@@ -107,15 +92,15 @@ def parse_isc(fileName):
else: else:
# 1.a. Single Line Definition # 1.a. Single Line Definition
inFunctionBody = 1 inFunctionBody = 1
currFuncName = m.group(1) currFuncName = m.group("name")
currFuncStartLine = lineNum + 1 currFuncStartLine = lineNum + 1
continue continue
if inFuncDefArgMultiLine == 1: if inFuncDefArgMultiLine == 1:
# 1.b. Multi Line Function Arguments # 1.b. Multi Line Function Arguments
m = re_funcDefArgLine.match(line) m = re_FuncDefArgLine.match(line)
if m is not None: if m is not None:
if m.group(1) == "": if m.group("openBrace") == "":
# Next line is still argument list # Next line is still argument list
continue continue
else: else:
...@@ -131,10 +116,10 @@ def parse_isc(fileName): ...@@ -131,10 +116,10 @@ def parse_isc(fileName):
# 2. Inside Function Body # 2. Inside Function Body
if inFunctionBody == 1: if inFunctionBody == 1:
# 2.a Look for labels # 2.a Look for labels
m = re_basicBlockLabel.match(line) m = re_Label.match(line)
if m is not None: if m is not None:
# 2.a.1 Record name of Basic Block # 2.a.1 Record name of Basic Block
currBasicBlockName = m.group(1) currBasicBlockName = m.group("label")
continue continue
# 2.a.2. Look for start of Basic Block # 2.a.2. Look for start of Basic Block
...@@ -168,30 +153,30 @@ def parse_isc(fileName): ...@@ -168,30 +153,30 @@ def parse_isc(fileName):
continue; continue;
# 2.b. look for goto instructions # 2.b. look for goto instructions
m = re_gotoLine.match(line) m = re_gotoStatement.match(line)
if m is not None: if m is not None:
# 2.b.1. List of targets of current basic block # 2.b.1. List of targets of current basic block
targetBlock = m.group(1) targetBlock = m.group("label")
listCurrBasicBlockTargets.append(targetBlock) listCurrBasicBlockTargets.append(targetBlock)
continue continue
# 2.c. look for function calls # 2.c. look for function calls
m = re_funcCallLine.match(line) m = re_functionCallStatement.match(line)
if m is not None: if m is not None:
# 2.c.1. List of functions called by current block # 2.c.1. List of functions called by current block
funcCallName = m.group(1) funcCallName = m.group("name")
listCurrBlockFuncCalls.append(funcCallName) listCurrBlockFuncCalls.append(funcCallName)
continue continue
# 2.d. look for return instructions # 2.d. look for return instructions
m = re_returnLine.match(line) m = re_returnStatement.match(line)
if m is not None: if m is not None:
# Flag to say current basic block returns # Flag to say current basic block returns
isCurrBasicBlockReturning = 1 isCurrBasicBlockReturning = 1
continue continue
# look for end of function definition # look for end of function definition
m = re_funcDefEnd.match(line) m = re_BlockEndRBrace.match(line)
if m is not None: if m is not None:
currFuncEndLine = lineNum - 1 currFuncEndLine = lineNum - 1
for blockTarget in listCurrFuncBasicBlockTargets: for blockTarget in listCurrFuncBasicBlockTargets:
......
...@@ -34,7 +34,8 @@ re_VarDeclInitMultiLineEnd = re.compile("\s*(?:%s)\s*;" % (VarSpecInitMultiLineE ...@@ -34,7 +34,8 @@ re_VarDeclInitMultiLineEnd = re.compile("\s*(?:%s)\s*;" % (VarSpecInitMultiLineE
VarSpec_ = "(?:%s)\s*(?:\w*)\s*(?:(?:\[.*\])*)?" % (DataTypes) VarSpec_ = "(?:%s)\s*(?:\w*)\s*(?:(?:\[.*\])*)?" % (DataTypes)
FuncParams = "(?:(?:%s)(?:\s*,\s*(?:%s))*)" % (VarSpec_, VarSpec_) FuncParams = "(?:(?:%s)(?:\s*,\s*(?:%s))*)" % (VarSpec_, VarSpec_)
RetTypes = "(?:%s)|void" % (DataTypes) 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)) re_FuncDefStart = re.compile("\s*(?P<retType>%s)?\s*(?P<name>\w*)\s*\((?P<params>%s)?(?P<openBrace>$|\)\s*\{)" % (RetTypes, FuncParams))
re_FuncDefArgLine = re.compile("\s*(?P<params>%s)(?P<openBrace>$|\)\s*\{)" % (FuncParams))
# Label # Label
re_Label = re.compile("\s*(?P<label>\w*):") re_Label = re.compile("\s*(?P<label>\w*):")
...@@ -44,7 +45,7 @@ re_ifStatement = re.compile("\s*if\s*\(.*\)") ...@@ -44,7 +45,7 @@ re_ifStatement = re.compile("\s*if\s*\(.*\)")
re_elseStatement = re.compile("\s*else\s*") re_elseStatement = re.compile("\s*else\s*")
# Goto Instructions # Goto Instructions
re_gotoStatement = re.compile("\s*goto\s*\w*;") re_gotoStatement = re.compile("\s*goto\s*(?P<label>\w*);")
# Struct Declaration Start # Struct Declaration Start
re_structDecl = re.compile("\s*struct\s*\w*\s*{.*};$") re_structDecl = re.compile("\s*struct\s*\w*\s*{.*};$")
...@@ -54,9 +55,14 @@ re_structDeclMultiLine = re.compile("\s*struct\s*\w*\s*{\s*$") ...@@ -54,9 +55,14 @@ re_structDeclMultiLine = re.compile("\s*struct\s*\w*\s*{\s*$")
re_BlockStartLBrace = re.compile("^\s*\{\s*$") re_BlockStartLBrace = re.compile("^\s*\{\s*$")
re_BlockEndRBrace = re.compile("^\s*\}\s*$") re_BlockEndRBrace = re.compile("^\s*\}\s*$")
re_returnStatement = re.compile("^\s*return\s*.*;\s*$") re_returnStatement = re.compile("^\s*return\s*.*;\s*$")
re_functionCallStatement = re.compile("\s*\w*\s*\(.*\);\s*") re_functionCallStatement = re.compile("\s*(?P<name>\w*)\s*\((?P<params>.*)\);\s*")
# [&\s,\w\[\]\(\)\*] # [&\s,\w\[\]\(\)\*]
# TODO: Improve if necessary
# Brought forward from old grammar
re_basicBlockStart = re.compile('\s*//\s*#\s*PRED:./*')
re_basicBlockEnd = re.compile('\s*//\s*#\s*SUCC:./*')
# #
# # Assignment/Arithmetic # # Assignment/Arithmetic
# Var = "(?:\s*\w*\s*)" # Var = "(?:\s*\w*\s*)"
......
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