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.C.TranslatorC Class Reference
+ Inheritance diagram for miasm2.ir.translators.C.TranslatorC:
+ Collaboration diagram for miasm2.ir.translators.C.TranslatorC:

Public Member Functions

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

Static Public Attributes

dictionary dct_shift
 
dictionary dct_rot
 
dictionary dct_div
 
list available_translators = []
 

Static Private Attributes

string __LANG__ = "C"
 

Detailed Description

Definition at line 6 of file C.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.C.TranslatorC.from_ExprAff (   self,
  expr 
)

Definition at line 40 of file C.py.

40 
41  def from_ExprAff(self, expr):
42  return "%s = %s" % tuple(map(self.from_expr, (expr.dst, expr.src)))

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.C.TranslatorC.from_ExprCompose (   self,
  expr 
)

Definition at line 146 of file C.py.

147  def from_ExprCompose(self, expr):
148  out = []
149  # XXX check mask for 64 bit & 32 bit compat
150  dst_cast = "uint%d_t" % expr.size
151  for x in expr.args:
152  out.append("(((%s)(%s & 0x%X)) << %d)" % (dst_cast,
153  self.from_expr(x[0]),
154  (1 << (x[2] - x[1])) - 1,
155  x[1]))
156  out = ' | '.join(out)
157  return '(' + out + ')'
158 
159 
160 # Register the class
161 Translator.register(TranslatorC)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.C.TranslatorC.from_ExprCond (   self,
  expr 
)

Definition at line 43 of file C.py.

43 
44  def from_ExprCond(self, expr):
45  return "(%s?%s:%s)" % tuple(map(self.from_expr,
46  (expr.cond, expr.src1, expr.src2)))

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.C.TranslatorC.from_ExprId (   self,
  expr 
)

Definition at line 32 of file C.py.

32 
33  def from_ExprId(self, expr):
34  if isinstance(expr.name, asmbloc.asm_label):
35  return "0x%x" % expr.name.offset
36  return str(expr)

+ Here is the caller graph for this function:

def miasm2.ir.translators.C.TranslatorC.from_ExprInt (   self,
  expr 
)

Definition at line 37 of file C.py.

37 
38  def from_ExprInt(self, expr):
39  return "0x%x" % expr.arg.arg

+ Here is the caller graph for this function:

def miasm2.ir.translators.C.TranslatorC.from_ExprMem (   self,
  expr 
)

Definition at line 47 of file C.py.

47 
48  def from_ExprMem(self, expr):
49  return "MEM_LOOKUP_%.2d(jitcpu, %s)" % (expr.size,
50  self.from_expr(expr.arg))

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.C.TranslatorC.from_ExprOp (   self,
  expr 
)

Definition at line 51 of file C.py.

51 
52  def from_ExprOp(self, expr):
53  if len(expr.args) == 1:
54  if expr.op == 'parity':
55  return "parity(%s&0x%x)" % (self.from_expr(expr.args[0]),
56  size2mask(expr.args[0].size))
57  elif expr.op in ['bsr', 'bsf']:
58  return "x86_%s(%s, 0x%x)" % (expr.op,
59  self.from_expr(expr.args[0]),
60  expr.args[0].size)
61  elif expr.op == '!':
62  return "(~ %s)&0x%x" % (self.from_expr(expr.args[0]),
63  size2mask(expr.args[0].size))
64  elif expr.op in ["hex2bcd", "bcd2hex"]:
65  return "%s_%d(%s)" % (expr.op, expr.args[0].size,
66  self.from_expr(expr.args[0]))
67  elif (expr.op.startswith("double_to_") or
68  expr.op.endswith("_to_double") or
69  expr.op.startswith("access_") or
70  expr.op.startswith("load_") or
71  expr.op in ["-", "ftan", "frndint", "f2xm1",
72  "fsin", "fsqrt", "fabs", "fcos"]):
73  return "%s(%s)" % (expr.op, self.from_expr(expr.args[0]))
74  else:
75  raise NotImplementedError('Unknown op: %r' % expr.op)
76 
77  elif len(expr.args) == 2:
78  if expr.op == "==":
79  return '(((%s&0x%x) == (%s&0x%x))?1:0)' % (
80  self.from_expr(expr.args[0]), size2mask(expr.args[0].size),
81  self.from_expr(expr.args[1]), size2mask(expr.args[1].size))
82  elif expr.op in self.dct_shift:
83  return 'shift_%s_%.2d(%s , %s)' % (self.dct_shift[expr.op],
84  expr.args[0].size,
85  self.from_expr(expr.args[0]),
86  self.from_expr(expr.args[1]))
87  elif expr.is_associative() or expr.op in ["%", "/"]:
88  oper = ['(%s&0x%x)' % (self.from_expr(arg), size2mask(arg.size))
89  for arg in expr.args]
90  oper = str(expr.op).join(oper)
91  return "((%s)&0x%x)" % (oper, size2mask(expr.args[0].size))
92  elif expr.op in ['-']:
93  return '(((%s&0x%x) %s (%s&0x%x))&0x%x)' % (
94  self.from_expr(expr.args[0]), size2mask(expr.args[0].size),
95  str(expr.op),
96  self.from_expr(expr.args[1]), size2mask(expr.args[1].size),
97  size2mask(expr.args[0].size))
98  elif expr.op in self.dct_rot:
99  return '(%s(%s, %s, %s) &0x%x)' % (self.dct_rot[expr.op],
100  expr.args[0].size,
101  self.from_expr(expr.args[0]),
102  self.from_expr(expr.args[1]),
103  size2mask(expr.args[0].size))
104  elif (expr.op.startswith('cpuid') or
105  expr.op.startswith("fcom") or
106  expr.op in ["fadd", "fsub", "fdiv", 'fmul', "fscale"]):
107  return "%s(%s, %s)" % (expr.op, self.from_expr(expr.args[0]),
108  self.from_expr(expr.args[1]))
109  elif expr.op == "segm":
110  return "segm2addr(jitcpu, %s, %s)" % (
111  self.from_expr(expr.args[0]), self.from_expr(expr.args[1]))
112  elif expr.op in ['udiv', 'umod', 'idiv', 'imod']:
113  return '%s%d((vm_cpu_t*)jitcpu->cpu, %s, %s)' % (expr.op,
114  expr.args[0].size,
115  self.from_expr(expr.args[0]),
116  self.from_expr(expr.args[1]))
117  elif expr.op in ["bcdadd", "bcdadd_cf"]:
118  return "%s_%d(%s, %s)" % (expr.op, expr.args[0].size,
119  self.from_expr(expr.args[0]),
120  self.from_expr(expr.args[1]))
121  else:
122  raise NotImplementedError('Unknown op: %r' % expr.op)
123 
124  elif len(expr.args) == 3 and expr.op in self.dct_div:
125  return '(%s(%s, %s, %s, %s) &0x%x)' % (self.dct_div[expr.op],
126  expr.args[0].size,
127  self.from_expr(expr.args[0]),
128  self.from_expr(expr.args[1]),
129  self.from_expr(expr.args[2]),
130  size2mask(expr.args[0].size))
131 
132  elif len(expr.args) >= 3 and expr.is_associative(): # ?????
133  oper = ['(%s&0x%x)' % (self.from_expr(arg), size2mask(arg.size))
134  for arg in expr.args]
135  oper = str(expr.op).join(oper)
136  return "((%s)&0x%x)" % (oper, size2mask(expr.args[0].size))
137 
138  else:
139  raise NotImplementedError('Unknown op: %s' % expr.op)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.ir.translators.C.TranslatorC.from_ExprSlice (   self,
  expr 
)

Definition at line 140 of file C.py.

141  def from_ExprSlice(self, expr):
142  # XXX check mask for 64 bit & 32 bit compat
143  return "((%s>>%d) & 0x%X)" % (self.from_expr(expr.arg),
144  expr.start,
145  (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.C.TranslatorC.__LANG__ = "C"
staticprivate

Definition at line 10 of file C.py.

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

Definition at line 9 of file translator.py.

dictionary miasm2.ir.translators.C.TranslatorC.dct_div
static
Initial value:
1 = {'div8': "div_op",
2  'div16': "div_op",
3  'div32': "div_op",
4  'idiv32': "div_op", # XXX to test
5  '<<<c_rez': 'rcl_rez_op',
6  '<<<c_cf': 'rcl_cf_op',
7  '>>>c_rez': 'rcr_rez_op',
8  '>>>c_cf': 'rcr_cf_op',
9  }

Definition at line 21 of file C.py.

dictionary miasm2.ir.translators.C.TranslatorC.dct_rot
static
Initial value:
1 = {'<<<': 'rot_left',
2  '>>>': 'rot_right',
3  }

Definition at line 18 of file C.py.

dictionary miasm2.ir.translators.C.TranslatorC.dct_shift
static
Initial value:
1 = {'a>>': "right_arith",
2  '>>': "right_logic",
3  '<<': "left_logic",
4  'a<<': "left_logic",
5  }

Definition at line 13 of file C.py.


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