Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
Public Member Functions | Static Public Attributes | Static Private Attributes | List of all members
miasm2.ir.translators.python.TranslatorPython Class Reference
+ Inheritance diagram for miasm2.ir.translators.python.TranslatorPython:
+ Collaboration diagram for miasm2.ir.translators.python.TranslatorPython:

Public Member Functions

def from_ExprInt
 
def from_ExprId
 
def from_ExprMem
 
def from_ExprSlice
 
def from_ExprCompose
 
def from_ExprCond
 
def from_ExprOp
 
def from_ExprAff
 
def register
 
def to_language
 
def available_languages
 
def from_expr
 

Static Public Attributes

list op_no_translate = ["+", "-", "/", "%", ">>", "<<", "&", "^", "|", "*"]
 
list available_translators = []
 

Static Private Attributes

string __LANG__ = "Python"
 

Detailed Description

Translate a Miasm expression to an equivalent Python code

Memory is abstracted using the unimplemented function:
int memory(int address, int size)

Definition at line 4 of file python.py.

Member Function Documentation

def miasm2.ir.translators.translator.Translator.available_languages (   cls)
inherited

Definition at line 34 of file translator.py.

34 
35  def available_languages(cls):
36  "Return the list of registered languages"
37  return [translator.__LANG__ for translator in cls.available_translators]
def miasm2.ir.translators.translator.Translator.from_expr (   self,
  expr 
)
inherited
Translate an expression according to its type
@expr: expression to translate

Definition at line 92 of file translator.py.

92 
93  def from_expr(self, expr):
94  """Translate an expression according to its type
95  @expr: expression to translate
96  """
97  # Use cache
98  if expr in self._cache:
99  return self._cache[expr]
100 
101  # Handle Expr type
102  handlers = {m2_expr.ExprInt: self.from_ExprInt,
103  m2_expr.ExprId: self.from_ExprId,
104  m2_expr.ExprCompose: self.from_ExprCompose,
105  m2_expr.ExprSlice: self.from_ExprSlice,
106  m2_expr.ExprOp: self.from_ExprOp,
107  m2_expr.ExprMem: self.from_ExprMem,
108  m2_expr.ExprAff: self.from_ExprAff,
109  m2_expr.ExprCond: self.from_ExprCond
110  }
111  for target, handler in handlers.iteritems():
112  if isinstance(expr, target):
113  ## Compute value and update the internal cache
114  ret = handler(expr)
115  self._cache[expr] = ret
116  return ret
117  raise ValueError("Unhandled type for %s" % expr)
118 

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprAff (   self,
  expr 
)

Definition at line 60 of file python.py.

60 
61  def from_ExprAff(self, expr):
62  return "%s = %s" % tuple(map(self.from_expr, (expr.dst, expr.src)))
63 
64 
65 # Register the class
66 Translator.register(TranslatorPython)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprCompose (   self,
  expr 
)

Definition at line 32 of file python.py.

32 
33  def from_ExprCompose(self, expr):
34  out = []
35  for subexpr, start, stop in expr.args:
36  out.append("((%s & 0x%x) << %d)" % (self.from_expr(subexpr),
37  (1 << (stop - start)) - 1,
38  start))
39  return "(%s)" % ' | '.join(out)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprCond (   self,
  expr 
)

Definition at line 40 of file python.py.

40 
41  def from_ExprCond(self, expr):
42  return "(%s if (%s) else %s)" % (self.from_expr(expr.src1),
43  self.from_expr(expr.cond),
44  self.from_expr(expr.src2))

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprId (   self,
  expr 
)

Definition at line 19 of file python.py.

19 
20  def from_ExprId(self, expr):
21  return str(expr)

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprInt (   self,
  expr 
)

Definition at line 16 of file python.py.

16 
17  def from_ExprInt(self, expr):
18  return str(expr)

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprMem (   self,
  expr 
)

Definition at line 22 of file python.py.

22 
23  def from_ExprMem(self, expr):
24  return "memory(%s, 0x%x)" % (self.from_expr(expr.arg),
25  expr.size / 8)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprOp (   self,
  expr 
)

Definition at line 45 of file python.py.

45 
46  def from_ExprOp(self, expr):
47  if expr.op in self.op_no_translate:
48  args = map(self.from_expr, expr.args)
49  if len(expr.args) == 1:
50  return "((%s %s) & 0x%x)" % (expr.op,
51  args[0],
52  (1 << expr.size) - 1)
53  else:
54  return "((%s) & 0x%x)" % ((" %s " % expr.op).join(args),
55  (1 << expr.size) - 1)
56  elif expr.op == "parity":
57  return "(%s & 0x1)" % self.from_expr(expr.args[0])
58 
59  raise NotImplementedError("Unknown operator: %s" % expr.op)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.python.TranslatorPython.from_ExprSlice (   self,
  expr 
)

Definition at line 26 of file python.py.

26 
27  def from_ExprSlice(self, expr):
28  out = self.from_expr(expr.arg)
29  if expr.start != 0:
30  out = "(%s >> %d)" % (out, expr.start)
31  return "(%s & 0x%x)" % (out, (1 << (expr.stop - expr.start)) - 1)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.translator.Translator.register (   cls,
  translator 
)
inherited
Register a translator
@translator: Translator sub-class

Definition at line 14 of file translator.py.

14 
15  def register(cls, translator):
16  """Register a translator
17  @translator: Translator sub-class
18  """
19  cls.available_translators.append(translator)
def miasm2.ir.translators.translator.Translator.to_language (   cls,
  target_lang,
  args,
  kwargs 
)
inherited
Return the corresponding translator instance
@target_lang: str (case insensitive) wanted language
Raise a NotImplementedError in case of unmatched language

Definition at line 21 of file translator.py.

21 
22  def to_language(cls, target_lang, *args, **kwargs):
23  """Return the corresponding translator instance
24  @target_lang: str (case insensitive) wanted language
25  Raise a NotImplementedError in case of unmatched language
26  """
27  target_lang = target_lang.lower()
28  for translator in cls.available_translators:
29  if translator.__LANG__.lower() == target_lang:
30  return translator(*args, **kwargs)
31 
32  raise NotImplementedError("Unknown target language: %s" % target_lang)
tuple translator
Definition: ir2C.py:15

Member Data Documentation

string miasm2.ir.translators.python.TranslatorPython.__LANG__ = "Python"
staticprivate

Definition at line 12 of file python.py.

list miasm2.ir.translators.translator.Translator.available_translators = []
staticinherited

Definition at line 9 of file translator.py.

list miasm2.ir.translators.python.TranslatorPython.op_no_translate = ["+", "-", "/", "%", ">>", "<<", "&", "^", "|", "*"]
static

Definition at line 14 of file python.py.


The documentation for this class was generated from the following file: