diff options
| author | Ajax <commial@gmail.com> | 2015-04-23 18:28:17 +0200 |
|---|---|---|
| committer | Ajax <commial@gmail.com> | 2015-04-23 18:28:17 +0200 |
| commit | 2f37ae22fcbd173a5554aa3fba922e7f9713cf83 (patch) | |
| tree | 1c031d2b977ed9d1b5df969ed9bfabafc3975a58 /miasm2/core/sembuilder.py | |
| parent | 8797f3e11ec42747ed1593924df22fd00271e85a (diff) | |
| download | miasm-2f37ae22fcbd173a5554aa3fba922e7f9713cf83.tar.gz miasm-2f37ae22fcbd173a5554aa3fba922e7f9713cf83.zip | |
SemBuilder: Add `('X' % Y)(Z) -> ExprOp('X' % Y, Z)` and `('X')(Y) -> ExprOp('X')(Y)`
Diffstat (limited to 'miasm2/core/sembuilder.py')
| -rw-r--r-- | miasm2/core/sembuilder.py | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/miasm2/core/sembuilder.py b/miasm2/core/sembuilder.py index 34271960..ba9d9d80 100644 --- a/miasm2/core/sembuilder.py +++ b/miasm2/core/sembuilder.py @@ -11,6 +11,8 @@ class MiasmTransformer(ast.NodeTransformer): memX[Y] -> ExprMem(Y, X) iX(Y) -> ExprIntX(Y) X if Y else Z -> ExprCond(Y, X, Z) + 'X'(Y) -> ExprOp('X', Y) + ('X' % Y)(Z) -> ExprOp('X' % Y, Z) """ # Parsers @@ -20,23 +22,40 @@ class MiasmTransformer(ast.NodeTransformer): # Visitors def visit_Call(self, node): - """iX(Y) -> ExprIntX(Y)""" - # Match the function name - if not isinstance(node.func, ast.Name): + """iX(Y) -> ExprIntX(Y), + 'X'(Y) -> ExprOp('X', Y), ('X' % Y)(Z) -> ExprOp('X' % Y, Z)""" + if isinstance(node.func, ast.Name): + # iX(Y) -> ExprIntX(Y) + fc_name = node.func.id + + # Match the function name + new_name = fc_name + integer = self.parse_integer.search(fc_name) + + # Do replacement + if integer is not None: + new_name = "ExprInt%s" % integer.groups()[0] + + # Replace in the node + node.func.id = new_name + + elif (isinstance(node.func, ast.Str) or + (isinstance(node.func, ast.BinOp) and + isinstance(node.func.op, ast.Mod) and + isinstance(node.func.left, ast.Str))): + # 'op'(args...) -> ExprOp('op', args...) + # ('op' % (fmt))(args...) -> ExprOp('op' % (fmt), args...) + op_name = node.func + + # Do replacement + node.func = ast.Name(id="ExprOp", ctx=ast.Load()) + node.args[0:0] = [op_name] + node.args = map(self.visit, node.args) + + else: # TODO: launch visitor on node - return node - - fc_name = node.func.id - new_name = fc_name - integer = self.parse_integer.search(fc_name) + pass - # Do replacement - if integer is not None: - new_name = "ExprInt%s" % integer.groups()[0] - - # Replace in the node - node.func.id = new_name - # TODO: launch visitor on node return node def visit_Subscript(self, node): @@ -104,6 +123,7 @@ class SemBuilder(object): if (isinstance(dst, ast.Name) and dst.id not in argument_names and dst.id not in self.ctx): + # Real variable declaration statement.value = src body.append(statement) |