Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Match compiler: Aggregate match functions with the same pattern / fun…

…ction call signature

Reduces the number of generated functions from 2290 to 1916.
  • Loading branch information...
commit 3918948dd5cd57489cad6c55b707e9b39ffefd65 1 parent 5f09cb2
@thomasjfox thomasjfox authored
Showing with 60 additions and 9 deletions.
  1. +60 −9 tools/matchcompiler.py
View
69 tools/matchcompiler.py
@@ -9,8 +9,45 @@ def __init__(self):
self._selftests()
def _reset(self):
- self._matchFunctions = []
+ self._rawMatchFunctions = []
self._matchStrs = {}
+ self._matchFunctionCache = {}
+
+ def _generateCacheSignature(self, pattern, endToken=None, varId=None, isFindMatch=False):
+ sig = pattern
+
+ if endToken:
+ sig += '|ENDTOKEN'
+ else:
+ sig += '|NO-ENDTOKEN'
+
+ if varId:
+ sig += '|VARID'
+ else:
+ sig += '|NO-VARID'
+
+ if isFindMatch:
+ sig += '|ISFINDMATCH'
+ else:
+ sig += '|NORMALMATCH'
+
+ return sig
+
+ def _lookupMatchFunctionId(self, pattern, endToken=None, varId=None, isFindMatch=False):
+ signature = self._generateCacheSignature(pattern, endToken, varId, isFindMatch)
+
+ if signature in self._matchFunctionCache:
+ return self._matchFunctionCache[signature]
+
+ return None
+
+ def _insertMatchFunctionId(self, id, pattern, endToken=None, varId=None, isFindMatch=False):
+ signature = self._generateCacheSignature(pattern, endToken, varId, isFindMatch)
+
+ # function signature should not be in the cache
+ assert(self._lookupMatchFunctionId(pattern, endToken, varId, isFindMatch) == None)
+
+ self._matchFunctionCache[signature] = id
def _insertMatchStr(self, look_for):
prefix = 'matchStr'
@@ -64,7 +101,7 @@ def _compilePattern(self, pattern, nr, varid, isFindMatch=False):
if varid:
arg2 = ', const unsigned int varid'
- ret = '// ' + pattern + '\n'
+ ret = '// pattern: ' + pattern + '\n'
ret += 'static bool match' + str(nr) + '(const Token *tok'+arg2+') {\n'
returnStatement = 'return false;\n'
@@ -145,7 +182,7 @@ def _compileFindPattern(self, pattern, findmatchnr, endToken, varId):
if varId:
more_args += ', unsigned int varid'
- ret = '// ' + pattern + '\n'
+ ret = '// pattern: ' + pattern + '\n'
ret += 'static const Token *findmatch' + str(findmatchnr) + '(const Token *start_tok'+more_args+') {\n'
ret += ' for (; start_tok' + endCondition + '; start_tok = start_tok->next()) {\n'
@@ -234,13 +271,20 @@ def _replaceTokenMatch(self, line):
if res == None:
break # Non-const pattern - bailout
else:
- arg2 = res.group(1)
+ pattern = res.group(1)
a3 = ''
if arg3:
a3 = ',' + arg3
- patternNumber = len(self._matchFunctions) + 1
+
+ # Compile function or use previously compiled one
+ patternNumber = self._lookupMatchFunctionId(pattern, None, arg3, False)
+
+ if patternNumber == None:
+ patternNumber = len(self._rawMatchFunctions) + 1
+ self._insertMatchFunctionId(patternNumber, pattern, None, arg3, False)
+ self._rawMatchFunctions.append(self._compilePattern(pattern, patternNumber, arg3))
+
line = line[:pos1]+'match'+str(patternNumber)+'('+arg1+a3+')'+line[pos1+len(g0):]
- self._matchFunctions.append(self._compilePattern(arg2, patternNumber, arg3))
return line
@@ -298,9 +342,16 @@ def _replaceTokenFindMatch(self, line):
a3 += ',' + endToken
if varId:
a3 += ',' + varId
- findMatchNumber = len(self._matchFunctions) + 1
+
+ # Compile function or use previously compiled one
+ findMatchNumber = self._lookupMatchFunctionId(pattern, endToken, varId, True)
+
+ if findMatchNumber == None:
+ findMatchNumber = len(self._rawMatchFunctions) + 1
+ self._insertMatchFunctionId(findMatchNumber, pattern, endToken, varId, True)
+ self._rawMatchFunctions.append(self._compileFindPattern(pattern, findMatchNumber, endToken, varId))
+
line = line[:pos1]+'findmatch'+str(findMatchNumber)+'('+arg1+a3+')'+line[pos1+len(g0):]
- self._matchFunctions.append(self._compileFindPattern(pattern, findMatchNumber, endToken, varId))
return line
@@ -356,7 +407,7 @@ def convertFile(self, srcname, destname):
# Compute matchFunctions
strFunctions = ''
- for function in self._matchFunctions:
+ for function in self._rawMatchFunctions:
strFunctions += function
fout = open(destname, 'wt')
Please sign in to comment.
Something went wrong with that request. Please try again.