Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
sem.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2011 EADS France, Fabrice Desclaux <fabrice.desclaux@eads.net>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #
18 
19 import miasm2.expression.expression as m2_expr
20 from miasm2.expression.simplifications import expr_simp
21 from miasm2.arch.x86.regs import *
22 from miasm2.arch.x86.arch import mn_x86, repeat_mn, replace_regs
23 from miasm2.expression.expression_helper import expr_cmps, expr_cmpu
24 from miasm2.ir.ir import ir, irbloc
25 import math
26 import struct
27 
28 # interrupt with eip update after instr
29 EXCEPT_SOFT_BP = (1 << 1)
30 EXCEPT_INT_XX = (1 << 2)
31 
32 EXCEPT_BREAKPOINT_INTERN = (1 << 10)
33 
34 EXCEPT_NUM_UPDT_EIP = (1 << 11)
35 # interrupt with eip at instr
36 EXCEPT_UNK_MEM_AD = (1 << 12)
37 EXCEPT_THROW_SEH = (1 << 13)
38 EXCEPT_UNK_EIP = (1 << 14)
39 EXCEPT_ACCESS_VIOL = (1 << 14)
40 EXCEPT_INT_DIV_BY_ZERO = (1 << 16)
41 EXCEPT_PRIV_INSN = (1 << 17)
42 EXCEPT_ILLEGAL_INSN = (1 << 18)
43 EXCEPT_UNK_MNEMO = (1 << 19)
44 
45 
46 """
47 http://www.emulators.com/docs/nx11_flags.htm
48 
49 CF(A+B) = (((A XOR B) XOR D) < 0) XOR (((A XOR D) AND NOT (A XOR B)) < 0)
50 CF(A-B) = (((A XOR B) XOR D) < 0) XOR (((A XOR D) AND (A XOR B)) < 0)
51 
52 OF(A+B) = ((A XOR D) AND NOT (A XOR B)) < 0
53 OF(A-B) = ((A XOR D) AND (A XOR B)) < 0
54 """
55 
56 
57 
58 # XXX TODO make default check against 0 or not 0 (same eq as in C)
59 
60 
62  return [m2_expr.ExprAff(zf, m2_expr.ExprCond(a, m2_expr.ExprInt_from(zf, 0),
63  m2_expr.ExprInt_from(zf, 1)))]
64 
65 
67  return [m2_expr.ExprAff(nf, a.msb())]
68 
69 
71  return [m2_expr.ExprAff(pf,
72  m2_expr.ExprOp('parity',
73  a & m2_expr.ExprInt_from(a, 0xFF)))]
74 
75 
77  return [m2_expr.ExprAff(af,
78  m2_expr.ExprCond((a & m2_expr.ExprInt_from(a,0x10)),
79  m2_expr.ExprInt_from(af, 1),
80  m2_expr.ExprInt_from(af, 0)))]
81 
82 
84  e = []
85  e += update_flag_zf(a)
86  e += update_flag_nf(a)
87  e += update_flag_pf(a)
88  return e
89 
90 
92  e = []
93  e += update_flag_znp(a)
94  e.append(m2_expr.ExprAff(of, m2_expr.ExprInt_from(of, 0)))
95  e.append(m2_expr.ExprAff(cf, m2_expr.ExprInt_from(cf, 0)))
96  return e
97 
98 
100  e = []
101  e += update_flag_znp(a)
102  return e
103 
104 
105 def check_ops_msb(a, b, c):
106  if not a or not b or not c or a != b or a != c:
107  raise ValueError('bad ops size %s %s %s' % (a, b, c))
108 
109 
110 def arith_flag(a, b, c):
111  a_s, b_s, c_s = a.size, b.size, c.size
112  check_ops_msb(a_s, b_s, c_s)
113  a_s, b_s, c_s = a.msb(), b.msb(), c.msb()
114  return a_s, b_s, c_s
115 
116 # checked: ok for adc add because b & c before +cf
117 
118 def update_flag_add_cf(op1, op2, res):
119  "Compute cf in @res = @op1 + @op2"
120  ret = (((op1 ^ op2) ^ res) ^ ((op1 ^ res) & (~(op1 ^ op2)))).msb()
121  return m2_expr.ExprAff(cf, ret)
122 
123 
124 def update_flag_add_of(op1, op2, res):
125  "Compute of in @res = @op1 + @op2"
126  return m2_expr.ExprAff(of, (((op1 ^ res) & (~(op1 ^ op2)))).msb())
127 
128 
129 # checked: ok for sbb add because b & c before +cf
130 def update_flag_sub_cf(op1, op2, res):
131  "Compote CF in @res = @op1 - @op2"
132  ret = (((op1 ^ op2) ^ res) ^ ((op1 ^ res) & (op1 ^ op2))).msb()
133  return m2_expr.ExprAff(cf, ret)
134 
135 
136 def update_flag_sub_of(op1, op2, res):
137  "Compote OF in @res = @op1 - @op2"
138  return m2_expr.ExprAff(of, (((op1 ^ res) & (op1 ^ op2))).msb())
139 
140 # z = x+y (+cf?)
141 
142 
143 def update_flag_add(x, y, z):
144  e = []
145  e.append(update_flag_add_cf(x, y, z))
146  e.append(update_flag_add_of(x, y, z))
147  return e
148 
149 # z = x-y (+cf?)
150 
151 
152 def update_flag_sub(x, y, z):
153  e = []
154  e.append(update_flag_sub_cf(x, y, z))
155  e.append(update_flag_sub_of(x, y, z))
156  return e
157 
158 
159 def set_float_cs_eip(instr):
160  e = []
161  # XXX TODO check float updt
162  e.append(m2_expr.ExprAff(float_eip,
163  m2_expr.ExprInt_from(float_eip, instr.offset)))
164  e.append(m2_expr.ExprAff(float_cs, CS))
165  return e
166 
167 def mem2double(arg):
168  """
169  Add float convertion if argument is an ExprMem
170  @arg: argument to tranform
171  """
172  if isinstance(arg, m2_expr.ExprMem):
173  if arg.size > 64:
174  raise NotImplementedError('float to long')
175  return m2_expr.ExprOp('mem_%.2d_to_double' % arg.size, arg)
176  else:
177  return arg
178 
179 def float_implicit_st0(arg1, arg2):
180  """
181  Generate full float operators if one argument is implicit (float_st0)
182  """
183  if arg2 is None:
184  arg2 = arg1
185  arg1 = float_st0
186  return arg1, arg2
187 
188 
189 def gen_jcc(ir, instr, cond, dst, jmp_if):
190  """
191  Macro to generate jcc semantic
192  @ir: ir instance
193  @instr: instruction
194  @cond: condtion of the jcc
195  @dst: the dstination if jcc is taken
196  @jmp_if: jump if/notif cond
197  """
198 
199  e = []
200  meip = mRIP[instr.mode]
201  next_lbl = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
202  if jmp_if:
203  dstA, dstB = dst, next_lbl
204  else:
205  dstA, dstB = next_lbl, dst
206  mn_dst = m2_expr.ExprCond(cond,
207  dstA.zeroExtend(instr.mode),
208  dstB.zeroExtend(instr.mode))
209  e.append(m2_expr.ExprAff(meip, mn_dst))
210  e.append(m2_expr.ExprAff(ir.IRDst, mn_dst))
211  return e, []
212 
213 
214 def gen_fcmov(ir, instr, cond, arg1, arg2, mov_if):
215  """Generate fcmov
216  @ir: ir instance
217  @instr: instruction instance
218  @cond: condition
219  @mov_if: invert condition if False"""
220 
221  lbl_do = m2_expr.ExprId(ir.gen_label(), instr.mode)
222  lbl_skip = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
223  if mov_if:
224  dstA, dstB = lbl_do, lbl_skip
225  else:
226  dstA, dstB = lbl_skip, lbl_do
227  e = []
228  e_do, extra_irs = [m2_expr.ExprAff(arg1, arg2)], []
229  e_do.append(m2_expr.ExprAff(ir.IRDst, lbl_skip))
230  e.append(m2_expr.ExprAff(ir.IRDst, m2_expr.ExprCond(cond, dstA, dstB)))
231  return e, [irbloc(lbl_do.name, [e_do])]
232 
233 
234 def gen_cmov(ir, instr, cond, arg1, arg2, mov_if):
235  """Generate cmov
236  @ir: ir instance
237  @instr: instruction instance
238  @cond: condition
239  @mov_if: invert condition if False"""
240 
241  lbl_do = m2_expr.ExprId(ir.gen_label(), instr.mode)
242  lbl_skip = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
243  if mov_if:
244  dstA, dstB = lbl_do, lbl_skip
245  else:
246  dstA, dstB = lbl_skip, lbl_do
247  e = []
248  e_do, extra_irs = mov(ir, instr, arg1, arg2)
249  e_do.append(m2_expr.ExprAff(ir.IRDst, lbl_skip))
250  e.append(m2_expr.ExprAff(ir.IRDst, m2_expr.ExprCond(cond, dstA, dstB)))
251  return e, [irbloc(lbl_do.name, [e_do])]
252 
253 
254 def mov(ir, instr, a, b):
255  if a in [ES, CS, SS, DS, FS, GS]:
256  b = b[:a.size]
257  if b in [ES, CS, SS, DS, FS, GS]:
258  b = b.zeroExtend(a.size)
259  e = [m2_expr.ExprAff(a, b)]
260  return e, []
261 
262 
263 def xchg(ir, instr, a, b):
264  e = []
265  e.append(m2_expr.ExprAff(a, b))
266  e.append(m2_expr.ExprAff(b, a))
267  return e, []
268 
269 
270 def movzx(ir, instr, a, b):
271  e = [m2_expr.ExprAff(a, b.zeroExtend(a.size))]
272  return e, []
273 
274 def movsx(ir, instr, a, b):
275  e = [m2_expr.ExprAff(a, b.signExtend(a.size))]
276  return e, []
277 
278 
279 def lea(ir, instr, a, b):
280  src = b.arg
281  if src.size > a.size:
282  src = src[:a.size]
283  e = [m2_expr.ExprAff(a, src.zeroExtend(a.size))]
284  return e, []
285 
286 
287 def add(ir, instr, a, b):
288  e = []
289  c = a + b
290  e += update_flag_arith(c)
291  e += update_flag_af(c)
292  e += update_flag_add(a, b, c)
293  e.append(m2_expr.ExprAff(a, c))
294  return e, []
295 
296 
297 def xadd(ir, instr, a, b):
298  e = []
299  c = a + b
300  e += update_flag_arith(c)
301  e += update_flag_af(c)
302  e += update_flag_add(b, a, c)
303  e.append(m2_expr.ExprAff(b, a))
304  e.append(m2_expr.ExprAff(a, c))
305  return e, []
306 
307 
308 def adc(ir, instr, a, b):
309  e = []
310  c = a + (b + m2_expr.ExprCompose([(m2_expr.ExprInt(0, a.size - 1),
311  1, a.size),
312  (cf, 0, 1)]))
313  e += update_flag_arith(c)
314  e += update_flag_af(c)
315  e += update_flag_add(a, b, c)
316  e.append(m2_expr.ExprAff(a, c))
317  return e, []
318 
319 
320 def sub(ir, instr, a, b):
321  e = []
322  c = a - b
323  e += update_flag_arith(c)
324  e += update_flag_af(c)
325  e += update_flag_sub(a, b, c)
326  e.append(m2_expr.ExprAff(a, c))
327  return e, []
328 
329 # a-(b+cf)
330 
331 
332 def sbb(ir, instr, a, b):
333  e = []
334  c = a - (b + m2_expr.ExprCompose([(m2_expr.ExprInt(0, a.size - 1),
335  1, a.size),
336  (cf, 0, 1)]))
337  e += update_flag_arith(c)
338  e += update_flag_af(c)
339  e += update_flag_sub(a, b, c)
340  e.append(m2_expr.ExprAff(a, c))
341  return e, []
342 
343 
344 def neg(ir, instr, b):
345  e = []
346  a = m2_expr.ExprInt_from(b, 0)
347 
348  c = a - b
349  e += update_flag_arith(c)
350  e += update_flag_sub(a, b, c)
351  e += update_flag_af(c)
352  e.append(m2_expr.ExprAff(b, c))
353  return e, []
354 
355 
356 def l_not(ir, instr, b):
357  e = []
358  c = ~b
359  e.append(m2_expr.ExprAff(b, c))
360  return e, []
361 
362 
363 def l_cmp(ir, instr, a, b):
364  e = []
365  c = a - b
366  e += update_flag_arith(c)
367  e += update_flag_sub(a, b, c)
368  e += update_flag_af(c)
369  return e, []
370 
371 
372 def xor(ir, instr, a, b):
373  e = []
374  c = a ^ b
375  e += update_flag_logic(c)
376  e.append(m2_expr.ExprAff(a, c))
377  return e, []
378 
379 
380 def pxor(ir, instr, a, b):
381  e = []
382  c = a ^ b
383  e.append(m2_expr.ExprAff(a, c))
384  return e, []
385 
386 def l_or(ir, instr, a, b):
387  e = []
388  c = a | b
389  e += update_flag_logic(c)
390  e.append(m2_expr.ExprAff(a, c))
391  return e, []
392 
393 
394 def l_and(ir, instr, a, b):
395  e = []
396  c = a & b
397  e += update_flag_logic(c)
398  e.append(m2_expr.ExprAff(a, c))
399  return e, []
400 
401 
402 def l_test(ir, instr, a, b):
403  e = []
404  c = a & b
405  e += update_flag_logic(c)
406  return e, []
407 
408 
409 
410 def get_shift(a, b):
411  # b.size must match a
412  b = b.zeroExtend(a.size)
413  if a.size == 64:
414  shift = b & m2_expr.ExprInt_from(b, 0x3f)
415  else:
416  shift = b & m2_expr.ExprInt_from(b, 0x1f)
417  shift = expr_simp(shift)
418  return shift
419 
420 
421 def l_rol(ir, instr, a, b):
422  e = []
423  shifter = get_shift(a, b)
424  c = m2_expr.ExprOp('<<<', a, shifter)
425 
426  new_cf = c[:1]
427  e.append(m2_expr.ExprAff(cf, new_cf))
428  # hack (only valid if b=1)
429  e.append(m2_expr.ExprAff(of, c.msb() ^ new_cf))
430  e.append(m2_expr.ExprAff(a, c))
431  return e, []
432 
433 
434 def l_ror(ir, instr, a, b):
435  e = []
436  shifter = get_shift(a, b)
437  c = m2_expr.ExprOp('>>>', a, shifter)
438 
439  e.append(m2_expr.ExprAff(cf, c.msb()))
440  # hack (only valid if b=1): when count == 1: a = msb-1(dest)
441  e.append(m2_expr.ExprAff(of, (c ^ a).msb()))
442  e.append(m2_expr.ExprAff(a, c))
443  return e, []
444 
445 
446 def rcl(ir, instr, a, b):
447  e = []
448  shifter = get_shift(a, b)
449  c = m2_expr.ExprOp('<<<c_rez', a, shifter, cf.zeroExtend(a.size))
450  new_cf = m2_expr.ExprOp('<<<c_cf', a, shifter, cf.zeroExtend(a.size))[:1]
451 
452  e.append(m2_expr.ExprAff(cf, new_cf))
453  # hack (only valid if b=1)
454  e.append(m2_expr.ExprAff(of, c.msb() ^ new_cf))
455  e.append(m2_expr.ExprAff(a, c))
456  return e, []
457 
458 
459 def rcr(ir, instr, a, b):
460  e = []
461  shifter = get_shift(a, b)
462  c = m2_expr.ExprOp('>>>c_rez', a, shifter, cf.zeroExtend(a.size))
463  new_cf = m2_expr.ExprOp('>>>c_cf', a, shifter, cf.zeroExtend(a.size))[:1]
464 
465  e.append(m2_expr.ExprAff(cf, new_cf))
466  # hack (only valid if b=1)
467  e.append(m2_expr.ExprAff(of, (a ^ c).msb()))
468  e.append(m2_expr.ExprAff(a, c))
469 
470  return e, []
471 
472 
473 def sar(ir, instr, a, b):
474 
475  shifter = get_shift(a, b)
476  c = m2_expr.ExprOp('a>>', a, shifter)
477 
478  lbl_do = m2_expr.ExprId(ir.gen_label(), instr.mode)
479  lbl_skip = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
480 
481  new_cf = m2_expr.ExprOp('a>>', a,(shifter - m2_expr.ExprInt_from(a, 1)))[:1]
482 
483  e_do = [
484  m2_expr.ExprAff(cf, new_cf),
485  m2_expr.ExprAff(of, m2_expr.ExprInt_from(of, 0)),
486  m2_expr.ExprAff(a, c),
487  ]
488 
489  e_do += update_flag_znp(c)
490 
491  # dont generate conditional shifter on constant
492  if isinstance(shifter, m2_expr.ExprInt):
493  if int(shifter.arg) != 0:
494  return e_do, []
495  else:
496  return [], []
497 
498  e_do.append(m2_expr.ExprAff(ir.IRDst, lbl_skip))
499 
500  e = []
501  e.append(m2_expr.ExprAff(ir.IRDst, m2_expr.ExprCond(shifter, lbl_do,
502  lbl_skip)))
503  return e, [irbloc(lbl_do.name, [e_do])]
504 
505 
506 def shr(ir, instr, a, b):
507 
508  shifter = get_shift(a, b)
509  c = m2_expr.ExprOp('>>', a, shifter)
510 
511  lbl_do = m2_expr.ExprId(ir.gen_label(), instr.mode)
512  lbl_skip = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
513 
514  new_cf = m2_expr.ExprOp('>>', a, (shifter - m2_expr.ExprInt_from(a, 1)))[:1]
515 
516  e_do = [
517  m2_expr.ExprAff(cf, new_cf),
518  m2_expr.ExprAff(of, m2_expr.ExprInt_from(of, 0)),
519  m2_expr.ExprAff(a, c),
520  ]
521 
522  e_do += update_flag_znp(c)
523 
524  # dont generate conditional shifter on constant
525  if isinstance(shifter, m2_expr.ExprInt):
526  if int(shifter.arg) != 0:
527  return e_do, []
528  else:
529  return [], []
530 
531  e_do.append(m2_expr.ExprAff(ir.IRDst, lbl_skip))
532 
533  e = []
534  e.append(m2_expr.ExprAff(ir.IRDst, m2_expr.ExprCond(shifter, lbl_do,
535  lbl_skip)))
536  return e, [irbloc(lbl_do.name, [e_do])]
537 
538 
539 def shrd_cl(ir, instr, a, b):
540  e = []
541  opmode, admode = s, instr.v_admode()
542  shifter = mRCX[instr.mode][:8].zeroExtend(a.size)
543  shifter &= m2_expr.ExprInt_from(a, 0x1f)
544  c = (a >> shifter) | (b << (m2_expr.ExprInt_from(a, a.size) - shifter))
545  new_cf = (a >> (shifter - m2_expr.ExprInt_from(a, 1)))[:1]
546  e.append(m2_expr.ExprAff(cf, m2_expr.ExprCond(shifter,
547  new_cf,
548  cf)
549  )
550  )
551  e.append(m2_expr.ExprAff(of, a.msb()))
552  e += update_flag_znp(c)
553  e.append(m2_expr.ExprAff(a, c))
554  return e, []
555 
556 
557 def shrd(ir, instr, a, b, c):
558  e = []
559  shifter = get_shift(a, c)
560 
561  d = (a >> shifter) | (b << (m2_expr.ExprInt_from(a, a.size) - shifter))
562  new_cf = (a >> (shifter - m2_expr.ExprInt_from(a, 1)))[:1]
563  e.append(m2_expr.ExprAff(cf, m2_expr.ExprCond(shifter,
564  new_cf,
565  cf)
566  )
567  )
568  e.append(m2_expr.ExprAff(of, a.msb()))
569  e += update_flag_znp(d)
570  e.append(m2_expr.ExprAff(a, d))
571  return e, []
572 
573 
574 def sal(ir, instr, a, b):
575  e = []
576  shifter = get_shift(a, b)
577  c = m2_expr.ExprOp('a<<', a, shifter)
578  new_cf = (a >> (m2_expr.ExprInt_from(a, a.size) - shifter))[:1]
579  e.append(m2_expr.ExprAff(cf, m2_expr.ExprCond(shifter,
580  new_cf,
581  cf)
582  )
583  )
584  e += update_flag_znp(c)
585  e.append(m2_expr.ExprAff(of, c.msb() ^ new_cf))
586  e.append(m2_expr.ExprAff(a, c))
587  return e, []
588 
589 
590 def shl(ir, instr, a, b):
591  e = []
592  shifter = get_shift(a, b)
593  c = a << shifter
594  new_cf = (a >> (m2_expr.ExprInt_from(a, a.size) - shifter))[:1]
595  e.append(m2_expr.ExprAff(cf, m2_expr.ExprCond(shifter,
596  new_cf,
597  cf)
598  )
599  )
600  e += update_flag_znp(c)
601  e.append(m2_expr.ExprAff(of, c.msb() ^ new_cf))
602  e.append(m2_expr.ExprAff(a, c))
603  return e, []
604 
605 
606 def shld_cl(ir, instr, a, b):
607  return shld(ir, instr, a, b, ecx)
608 
609 
610 def shld(ir, instr, a, b, c):
611  e = []
612  shifter = c.zeroExtend(a.size) & m2_expr.ExprInt_from(a, 0x1f)
613  c = m2_expr.ExprOp('|',
614  a << shifter,
615  b >> (m2_expr.ExprInt_from(a, a.size) - shifter)
616  )
617 
618  new_cf = (a >> (m2_expr.ExprInt_from(a, a.size) - shifter))[:1]
619  e.append(m2_expr.ExprAff(cf, m2_expr.ExprCond(shifter,
620  new_cf,
621  cf)
622  )
623  )
624  # XXX todo: don't update flag if shifter is 0
625  e += update_flag_znp(c)
626  e.append(m2_expr.ExprAff(of, c.msb() ^ new_cf))
627  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(shifter,
628  c,
629  a)))
630  return e, []
631 
632 
633 # XXX todo ###
634 def cmc(ir, instr):
635  e = [m2_expr.ExprAff(cf, m2_expr.ExprCond(cf, m2_expr.ExprInt_from(cf, 0),
636  m2_expr.ExprInt_from(cf, 1)))]
637  return e, []
638 
639 
640 def clc(ir, instr):
641  e = [m2_expr.ExprAff(cf, m2_expr.ExprInt_from(cf, 0))]
642  return e, []
643 
644 
645 def stc(ir, instr):
646  e = [m2_expr.ExprAff(cf, m2_expr.ExprInt_from(cf, 1))]
647  return e, []
648 
649 
650 def cld(ir, instr):
651  e = [m2_expr.ExprAff(df, m2_expr.ExprInt_from(df, 0))]
652  return e, []
653 
654 
655 def std(ir, instr):
656  e = [m2_expr.ExprAff(df, m2_expr.ExprInt_from(df, 1))]
657  return e, []
658 
659 
660 def cli(ir, instr):
661  e = [m2_expr.ExprAff(i_f, m2_expr.ExprInt_from(i_f, 0))]
662  return e, []
663 
664 
665 def sti(ir, instr):
666  e = [m2_expr.ExprAff(exception_flags, m2_expr.ExprInt32(EXCEPT_PRIV_INSN))]
667  e = [] # XXX TODO HACK
668  return e, []
669 
670 
671 def inc(ir, instr, a):
672  e = []
673  b = m2_expr.ExprInt_from(a, 1)
674  c = a + b
675  e += update_flag_arith(c)
676  e += update_flag_af(c)
677 
678  e.append(update_flag_add_of(a, b, c))
679  e.append(m2_expr.ExprAff(a, c))
680  return e, []
681 
682 def dec(ir, instr, a):
683  e = []
684  b = m2_expr.ExprInt_from(a, -1)
685  c = a + b
686  e += update_flag_arith(c)
687  e += update_flag_af(c)
688 
689  e.append(update_flag_add_of(a, b, c))
690  e.append(m2_expr.ExprAff(a, c))
691  return e, []
692 
693 
694 def push_gen(ir, instr, a, size):
695  e = []
696  if not size in [16, 32, 64]:
697  raise ValueError('bad size stacker!')
698  if a.size < size:
699  a = a.zeroExtend(size)
700  elif a.size == size:
701  pass
702  else:
703  raise ValueError('strange arg size')
704 
705  sp = mRSP[instr.mode]
706  new_sp = sp - m2_expr.ExprInt_from(sp, size / 8)
707  e.append(m2_expr.ExprAff(sp, new_sp))
708  if ir.do_stk_segm:
709  new_sp = m2_expr.ExprOp('segm', SS, new_sp)
710  e.append(m2_expr.ExprAff(m2_expr.ExprMem(new_sp, size), a))
711  return e, []
712 
713 def push(ir, instr, a):
714  return push_gen(ir, instr, a, instr.mode)
715 
716 def pushw(ir, instr, a):
717  return push_gen(ir, instr, a, 16)
718 
719 
720 def pop_gen(ir, instr, a, size):
721  e = []
722  if not size in [16, 32, 64]:
723  raise ValueError('bad size stacker!')
724 
725  sp = mRSP[instr.mode]
726  new_sp = sp + m2_expr.ExprInt_from(sp, size / 8)
727  # don't generate ESP incrementation on POP ESP
728  if a != ir.sp:
729  e.append(m2_expr.ExprAff(sp, new_sp))
730  # XXX FIX XXX for pop [esp]
731  if isinstance(a, m2_expr.ExprMem):
732  a = a.replace_expr({sp: new_sp})
733  c = sp
734  if ir.do_stk_segm:
735  c = m2_expr.ExprOp('segm', SS, c)
736  e.append(m2_expr.ExprAff(a, m2_expr.ExprMem(c, a.size)))
737  return e, []
738 
739 def pop(ir, instr, a):
740  return pop_gen(ir, instr, a, instr.mode)
741 
742 def popw(ir, instr, a):
743  return pop_gen(ir, instr, a, 16)
744 
745 
746 def sete(ir, instr, a):
747  e = []
748  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(zf, m2_expr.ExprInt_from(a, 1),
749  m2_expr.ExprInt_from(a, 0))))
750  return e, []
751 
752 
753 def setnz(ir, instr, a):
754  e = []
755  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(zf, m2_expr.ExprInt_from(a, 0),
756  m2_expr.ExprInt_from(a, 1))))
757  return e, []
758 
759 
760 def setl(ir, instr, a):
761  e = []
762  e.append(
763  m2_expr.ExprAff(a, m2_expr.ExprCond(nf - of, m2_expr.ExprInt_from(a, 1),
764  m2_expr.ExprInt_from(a, 0))))
765  return e, []
766 
767 
768 def setg(ir, instr, a):
769  e = []
770  a0 = m2_expr.ExprInt_from(a, 0)
771  a1 = m2_expr.ExprInt_from(a, 1)
772  ret = m2_expr.ExprCond(zf, a0, a1) & m2_expr.ExprCond(nf - of, a0, a1)
773  e.append(m2_expr.ExprAff(a, ret))
774  return e, []
775 
776 
777 def setge(ir, instr, a):
778  e = []
779  e.append(
780  m2_expr.ExprAff(a, m2_expr.ExprCond(nf - of, m2_expr.ExprInt_from(a, 0),
781  m2_expr.ExprInt_from(a, 1))))
782  return e, []
783 
784 
785 def seta(ir, instr, a):
786  e = []
787  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(cf | zf,
788  m2_expr.ExprInt_from(a, 0),
789  m2_expr.ExprInt_from(a, 1))))
790 
791  return e, []
792 
793 
794 def setae(ir, instr, a):
795  e = []
796  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(cf, m2_expr.ExprInt_from(a, 0),
797  m2_expr.ExprInt_from(a, 1))))
798  return e, []
799 
800 
801 def setb(ir, instr, a):
802  e = []
803  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(cf, m2_expr.ExprInt_from(a, 1),
804  m2_expr.ExprInt_from(a, 0))))
805  return e, []
806 
807 
808 def setbe(ir, instr, a):
809  e = []
810  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(cf | zf,
811  m2_expr.ExprInt_from(a, 1),
812  m2_expr.ExprInt_from(a, 0)))
813  )
814  return e, []
815 
816 
817 def setns(ir, instr, a):
818  e = []
819  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(nf, m2_expr.ExprInt_from(a, 0),
820  m2_expr.ExprInt_from(a, 1))))
821  return e, []
822 
823 
824 def sets(ir, instr, a):
825  e = []
826  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(nf, m2_expr.ExprInt_from(a, 1),
827  m2_expr.ExprInt_from(a, 0))))
828  return e, []
829 
830 
831 def seto(ir, instr, a):
832  e = []
833  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(of, m2_expr.ExprInt_from(a, 1),
834  m2_expr.ExprInt_from(a, 0))))
835  return e, []
836 
837 
838 def setp(ir, instr, a):
839  e = []
840  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(pf, m2_expr.ExprInt_from(a, 1),
841  m2_expr.ExprInt_from(a, 0))))
842  return e, []
843 
844 
845 def setnp(ir, instr, a):
846  e = []
847  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(pf, m2_expr.ExprInt_from(a, 0),
848  m2_expr.ExprInt_from(a, 1))))
849  return e, []
850 
851 
852 def setle(ir, instr, a):
853  e = []
854  a0 = m2_expr.ExprInt_from(a, 0)
855  a1 = m2_expr.ExprInt_from(a, 1)
856  ret = m2_expr.ExprCond(zf, a1, a0) | m2_expr.ExprCond(nf ^ of, a1, a0)
857  e.append(m2_expr.ExprAff(a, ret))
858  return e, []
859 
860 
861 def setna(ir, instr, a):
862  e = []
863  a0 = m2_expr.ExprInt_from(a, 0)
864  a1 = m2_expr.ExprInt_from(a, 1)
865  ret = m2_expr.ExprCond(cf, a1, a0) & m2_expr.ExprCond(zf, a1, a0)
866  e.append(m2_expr.ExprAff(a, ret))
867  return e, []
868 
869 
870 def setnbe(ir, instr, a):
871  e = []
872  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(cf | zf,
873  m2_expr.ExprInt_from(a, 0),
874  m2_expr.ExprInt_from(a, 1)))
875  )
876  return e, []
877 
878 
879 def setno(ir, instr, a):
880  e = []
881  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(of, m2_expr.ExprInt_from(a, 0),
882  m2_expr.ExprInt_from(a, 1))))
883  return e, []
884 
885 
886 def setnb(ir, instr, a):
887  e = []
888  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(cf, m2_expr.ExprInt_from(a, 0),
889  m2_expr.ExprInt_from(a, 1))))
890  return e, []
891 
892 
893 def setalc(ir, instr):
894  a = mRAX[instr.mode][0:8]
895  e = []
896  e.append(
897  m2_expr.ExprAff(a, m2_expr.ExprCond(cf, m2_expr.ExprInt_from(a, 0xff),
898  m2_expr.ExprInt_from(a, 0))))
899  return e, []
900 
901 
902 def bswap(ir, instr, a):
903  e = []
904  if a.size == 16:
905  c = m2_expr.ExprCompose([(a[:8], 8, 16),
906  (a[8:16], 0, 8),
907  ])
908  elif a.size == 32:
909  c = m2_expr.ExprCompose([(a[:8], 24, 32),
910  (a[8:16], 16, 24),
911  (a[16:24], 8, 16),
912  (a[24:32], 0, 8),
913  ])
914  elif a.size == 64:
915  c = m2_expr.ExprCompose([(a[:8], 56, 64),
916  (a[8:16], 48, 56),
917  (a[16:24], 40, 48),
918  (a[24:32], 32, 40),
919  (a[32:40], 24, 32),
920  (a[40:48], 16, 24),
921  (a[48:56], 8, 16),
922  (a[56:64], 0, 8),
923  ])
924  else:
925  raise ValueError('the size DOES matter')
926  e.append(m2_expr.ExprAff(a, c))
927  return e, []
928 
929 
930 def cmps(ir, instr, size):
931  lbl_cmp = m2_expr.ExprId(ir.gen_label(), instr.mode)
932  lbl_df_0 = m2_expr.ExprId(ir.gen_label(), instr.mode)
933  lbl_df_1 = m2_expr.ExprId(ir.gen_label(), instr.mode)
934  lbl_next = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
935 
936  s = instr.v_admode()
937  a = m2_expr.ExprMem(mRDI[instr.mode][:s], size)
938  b = m2_expr.ExprMem(mRSI[instr.mode][:s], size)
939 
940  e, extra = l_cmp(ir, instr, a, b)
941 
942  e0 = []
943  e0.append(m2_expr.ExprAff(a.arg,
944  a.arg + m2_expr.ExprInt_from(a.arg, size / 8)))
945  e0.append(m2_expr.ExprAff(b.arg,
946  b.arg + m2_expr.ExprInt_from(b.arg, size / 8)))
947  e0.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
948  e0 = irbloc(lbl_df_0.name, [e0])
949 
950  e1 = []
951  e1.append(m2_expr.ExprAff(a.arg,
952  a.arg - m2_expr.ExprInt_from(a.arg, size / 8)))
953  e1.append(m2_expr.ExprAff(b.arg,
954  b.arg - m2_expr.ExprInt_from(b.arg, size / 8)))
955  e1.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
956  e1 = irbloc(lbl_df_1.name, [e1])
957 
958  e.append(m2_expr.ExprAff(ir.IRDst,
959  m2_expr.ExprCond(df, lbl_df_1, lbl_df_0)))
960  return e, [e0, e1]
961 
962 
963 def scas(ir, instr, size):
964  lbl_cmp = m2_expr.ExprId(ir.gen_label(), instr.mode)
965  lbl_df_0 = m2_expr.ExprId(ir.gen_label(), instr.mode)
966  lbl_df_1 = m2_expr.ExprId(ir.gen_label(), instr.mode)
967  lbl_next = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
968 
969  s = instr.v_admode()
970  a = m2_expr.ExprMem(mRDI[instr.mode][:s], size)
971 
972  e, extra = l_cmp(ir, instr, mRAX[instr.mode][:size], a)
973 
974  e0 = []
975  e0.append(m2_expr.ExprAff(a.arg,
976  a.arg + m2_expr.ExprInt_from(a.arg, size / 8)))
977  e0.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
978  e0 = irbloc(lbl_df_0.name, [e0])
979 
980  e1 = []
981  e1.append(m2_expr.ExprAff(a.arg,
982  a.arg - m2_expr.ExprInt_from(a.arg, size / 8)))
983  e1.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
984  e1 = irbloc(lbl_df_1.name, [e1])
985 
986  e.append(m2_expr.ExprAff(ir.IRDst,
987  m2_expr.ExprCond(df, lbl_df_1, lbl_df_0)))
988 
989  return e, [e0, e1]
990 
991 
992 def compose_eflag(s=32):
993  args = []
994 
995  regs = [cf, m2_expr.ExprInt1(1), pf, m2_expr.ExprInt1(
996  0), af, m2_expr.ExprInt1(0), zf, nf, tf, i_f, df, of]
997  for i in xrange(len(regs)):
998  args.append((regs[i], i, i + 1))
999 
1000  args.append((iopl, 12, 14))
1001 
1002  if s == 32:
1003  regs = [nt, m2_expr.ExprInt1(0), rf, vm, ac, vif, vip, i_d]
1004  elif s == 16:
1005  regs = [nt, m2_expr.ExprInt1(0)]
1006  else:
1007  raise ValueError('unk size')
1008  for i in xrange(len(regs)):
1009  args.append((regs[i], i + 14, i + 15))
1010  if s == 32:
1011  args.append((m2_expr.ExprInt(0, 10), 22, 32))
1012  return m2_expr.ExprCompose(args)
1013 
1014 
1015 def pushfd(ir, instr):
1016  return push(ir, instr, compose_eflag())
1017 
1018 def pushfq(ir, instr):
1019  return push(ir, instr, compose_eflag().zeroExtend(64))
1020 
1021 def pushfw(ir, instr):
1022  return pushw(ir, instr, compose_eflag(16))
1023 
1024 
1025 def popfd(ir, instr):
1026  tmp = m2_expr.ExprMem(mRSP[instr.mode])
1027  e = []
1028  e.append(m2_expr.ExprAff(cf, m2_expr.ExprSlice(tmp, 0, 1)))
1029  e.append(m2_expr.ExprAff(pf, m2_expr.ExprSlice(tmp, 2, 3)))
1030  e.append(m2_expr.ExprAff(af, m2_expr.ExprSlice(tmp, 4, 5)))
1031  e.append(m2_expr.ExprAff(zf, m2_expr.ExprSlice(tmp, 6, 7)))
1032  e.append(m2_expr.ExprAff(nf, m2_expr.ExprSlice(tmp, 7, 8)))
1033  e.append(m2_expr.ExprAff(tf, m2_expr.ExprSlice(tmp, 8, 9)))
1034  e.append(m2_expr.ExprAff(i_f, m2_expr.ExprSlice(tmp, 9, 10)))
1035  e.append(m2_expr.ExprAff(df, m2_expr.ExprSlice(tmp, 10, 11)))
1036  e.append(m2_expr.ExprAff(of, m2_expr.ExprSlice(tmp, 11, 12)))
1037  e.append(m2_expr.ExprAff(iopl, m2_expr.ExprSlice(tmp, 12, 14)))
1038  e.append(m2_expr.ExprAff(nt, m2_expr.ExprSlice(tmp, 14, 15)))
1039  e.append(m2_expr.ExprAff(rf, m2_expr.ExprSlice(tmp, 16, 17)))
1040  e.append(m2_expr.ExprAff(vm, m2_expr.ExprSlice(tmp, 17, 18)))
1041  e.append(m2_expr.ExprAff(ac, m2_expr.ExprSlice(tmp, 18, 19)))
1042  e.append(m2_expr.ExprAff(vif, m2_expr.ExprSlice(tmp, 19, 20)))
1043  e.append(m2_expr.ExprAff(vip, m2_expr.ExprSlice(tmp, 20, 21)))
1044  e.append(m2_expr.ExprAff(i_d, m2_expr.ExprSlice(tmp, 21, 22)))
1045  e.append(m2_expr.ExprAff(mRSP[instr.mode],
1046  mRSP[instr.mode] + m2_expr.ExprInt_from(mRSP[instr.mode], instr.mode/8)))
1047  e.append(m2_expr.ExprAff(exception_flags,
1048  m2_expr.ExprCond(m2_expr.ExprSlice(tmp, 8, 9),
1049  m2_expr.ExprInt32(EXCEPT_SOFT_BP),
1050  exception_flags
1051  )
1052  )
1053  )
1054  return e, []
1055 
1056 
1057 def popfw(ir, instr):
1058  tmp = m2_expr.ExprMem(mRSP[instr.mode])
1059  e = []
1060  e.append(m2_expr.ExprAff(cf, m2_expr.ExprSlice(tmp, 0, 1)))
1061  e.append(m2_expr.ExprAff(pf, m2_expr.ExprSlice(tmp, 2, 3)))
1062  e.append(m2_expr.ExprAff(af, m2_expr.ExprSlice(tmp, 4, 5)))
1063  e.append(m2_expr.ExprAff(zf, m2_expr.ExprSlice(tmp, 6, 7)))
1064  e.append(m2_expr.ExprAff(nf, m2_expr.ExprSlice(tmp, 7, 8)))
1065  e.append(m2_expr.ExprAff(tf, m2_expr.ExprSlice(tmp, 8, 9)))
1066  e.append(m2_expr.ExprAff(i_f, m2_expr.ExprSlice(tmp, 9, 10)))
1067  e.append(m2_expr.ExprAff(df, m2_expr.ExprSlice(tmp, 10, 11)))
1068  e.append(m2_expr.ExprAff(of, m2_expr.ExprSlice(tmp, 11, 12)))
1069  e.append(m2_expr.ExprAff(iopl, m2_expr.ExprSlice(tmp, 12, 14)))
1070  e.append(m2_expr.ExprAff(nt, m2_expr.ExprSlice(tmp, 14, 15)))
1071  e.append(m2_expr.ExprAff(mRSP[instr.mode], mRSP[instr.mode] + m2_expr.ExprInt(2, mRSP[instr.mode].size)))
1072  return e, []
1073 
1074 
1075 def pushad(ir, instr):
1076  e = []
1077  s = instr.v_opmode()
1078  opmode, admode = s, instr.v_admode()
1079  if not s in [16, 32, 64]:
1080  raise ValueError('bad size stacker!')
1081 
1082  regs = [
1083  mRAX[instr.mode][:s], mRCX[instr.mode][
1084  :s], mRDX[instr.mode][:s], mRBX[instr.mode][:s],
1085  mRSP[instr.mode][:s], mRBP[instr.mode][:s],
1086  mRSI[instr.mode][:s], mRDI[instr.mode][:s]]
1087 
1088  for i in xrange(len(regs)):
1089  c = mRSP[instr.mode][:s] + m2_expr.ExprInt(-(s / 8) * (i + 1), s)
1090  e.append(m2_expr.ExprAff(m2_expr.ExprMem(c, s), regs[i]))
1091  e.append(m2_expr.ExprAff(mRSP[instr.mode][:s], c))
1092  return e, []
1093 
1094 
1095 def popad(ir, instr):
1096  e = []
1097  s = instr.v_opmode()
1098  opmode, admode = s, instr.v_admode()
1099  if not s in [16, 32, 64]:
1100  raise ValueError('bad size stacker!')
1101  regs = [
1102  mRAX[instr.mode][:s], mRCX[instr.mode][
1103  :s], mRDX[instr.mode][:s], mRBX[instr.mode][:s],
1104  mRSP[instr.mode][:s], mRBP[instr.mode][:s],
1105  mRSI[instr.mode][:s], mRDI[instr.mode][:s]]
1106  myesp = mRSP[instr.mode][:s]
1107  regs.reverse()
1108  for i in xrange(len(regs)):
1109  if regs[i] == myesp:
1110  continue
1111  c = myesp + m2_expr.ExprInt_from(myesp, ((s / 8) * i))
1112  e.append(m2_expr.ExprAff(regs[i], m2_expr.ExprMem(c, s)))
1113 
1114  c = myesp + m2_expr.ExprInt_from(myesp, ((s / 8) * (i + 1)))
1115  e.append(m2_expr.ExprAff(myesp, c))
1116 
1117  return e, []
1118 
1119 
1120 def call(ir, instr, dst):
1121  e = []
1122  # opmode, admode = instr.opmode, instr.admode
1123  s = dst.size
1124  meip = mRIP[instr.mode]
1125  opmode, admode = s, instr.v_admode()
1126  myesp = mRSP[instr.mode][:opmode]
1127  n = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
1128 
1129 
1130  if (isinstance(dst, m2_expr.ExprOp) and dst.op == "segm"):
1131  # call far
1132  if instr.mode != 16:
1133  raise NotImplementedError('add 32 bit support!')
1134  segm = dst.args[0]
1135  base = dst.args[1]
1136  m1 = segm.zeroExtend(CS.size)
1137  m2 = base.zeroExtend(meip.size)
1138  e.append(m2_expr.ExprAff(CS, m1))
1139  e.append(m2_expr.ExprAff(meip, m2))
1140 
1141  e.append(m2_expr.ExprAff(ir.IRDst, m2))
1142 
1143  c = myesp + m2_expr.ExprInt(-s/8, s)
1144  e.append(m2_expr.ExprAff(m2_expr.ExprMem(c, size=s).zeroExtend(s),
1145  CS.zeroExtend(s)))
1146 
1147  c = myesp + m2_expr.ExprInt(-2*s/8, s)
1148  e.append(m2_expr.ExprAff(m2_expr.ExprMem(c, size=s).zeroExtend(s),
1149  meip.zeroExtend(s)))
1150 
1151  c = myesp + m2_expr.ExprInt((-2*s) / 8, s)
1152  e.append(m2_expr.ExprAff(myesp, c))
1153  return e, []
1154 
1155 
1156  c = myesp + m2_expr.ExprInt((-s / 8), s)
1157  e.append(m2_expr.ExprAff(myesp, c))
1158  if ir.do_stk_segm:
1159  c = m2_expr.ExprOp('segm', SS, c)
1160  e.append(m2_expr.ExprAff(m2_expr.ExprMem(c, size=s), n))
1161  e.append(m2_expr.ExprAff(meip, dst.zeroExtend(instr.mode)))
1162  e.append(m2_expr.ExprAff(ir.IRDst, dst.zeroExtend(instr.mode)))
1163  #if not expr_is_int_or_label(dst):
1164  # dst = meip
1165  return e, []
1166 
1167 
1168 def ret(ir, instr, a=None):
1169  e = []
1170  s = instr.mode
1171  meip = mRIP[instr.mode]
1172  opmode, admode = instr.v_opmode(), instr.v_admode()
1173  s = opmode
1174  myesp = mRSP[instr.mode][:s]
1175 
1176  if a is None:
1177  a = m2_expr.ExprInt(0, s)
1178  value = (myesp + (m2_expr.ExprInt((s / 8), s)))
1179  else:
1180  a = a.zeroExtend(s)
1181  value = (myesp + (m2_expr.ExprInt((s / 8), s) + a))
1182 
1183  e.append(m2_expr.ExprAff(myesp, value))
1184  c = myesp
1185  if ir.do_stk_segm:
1186  c = m2_expr.ExprOp('segm', SS, c)
1187  e.append(m2_expr.ExprAff(meip, m2_expr.ExprMem(c, size=s).zeroExtend(s)))
1188  e.append(m2_expr.ExprAff(ir.IRDst,
1189  m2_expr.ExprMem(c, size=s).zeroExtend(s)))
1190  return e, []
1191 
1192 
1193 def retf(ir, instr, a=None):
1194  e = []
1195  s = instr.mode
1196  meip = mRIP[instr.mode]
1197  opmode, admode = instr.v_opmode(), instr.v_admode()
1198  if a is None:
1199  a = m2_expr.ExprInt(0, s)
1200  s = opmode
1201  myesp = mRSP[instr.mode][:s]
1202 
1203  a = a.zeroExtend(s)
1204 
1205  c = myesp
1206  if ir.do_stk_segm:
1207  c = m2_expr.ExprOp('segm', SS, c)
1208  e.append(m2_expr.ExprAff(meip, m2_expr.ExprMem(c, size=s).zeroExtend(s)))
1209  e.append(m2_expr.ExprAff(ir.IRDst,
1210  m2_expr.ExprMem(c, size=s).zeroExtend(s)))
1211  # e.append(m2_expr.ExprAff(meip, m2_expr.ExprMem(c, size = s)))
1212  c = myesp + m2_expr.ExprInt(s / 8, s)
1213  if ir.do_stk_segm:
1214  c = m2_expr.ExprOp('segm', SS, c)
1215  e.append(m2_expr.ExprAff(CS, m2_expr.ExprMem(c, size=16)))
1216 
1217  value = myesp + (m2_expr.ExprInt((2*s) / 8, s) + a)
1218  e.append(m2_expr.ExprAff(myesp, value))
1219  return e, []
1220 
1221 
1222 def leave(ir, instr):
1223  opmode, admode = instr.v_opmode(), instr.v_admode()
1224  size = instr.mode
1225  myesp = mRSP[size]
1226  e = []
1227  e.append(m2_expr.ExprAff(mRBP[size],
1228  m2_expr.ExprMem(mRBP[size], size=size)))
1229  e.append(m2_expr.ExprAff(myesp,
1230  m2_expr.ExprInt(size / 8, size) + mRBP[size]))
1231  return e, []
1232 
1233 
1234 def enter(ir, instr, a, b):
1235  opmode, admode = instr.v_opmode(), instr.v_admode()
1236  s = opmode
1237  myesp = mRSP[instr.mode][:s]
1238  myebp = mRBP[instr.mode][:s]
1239 
1240  a = a.zeroExtend(s)
1241 
1242  e = []
1243  esp_tmp = myesp - m2_expr.ExprInt(s / 8, s)
1244  e.append(m2_expr.ExprAff(m2_expr.ExprMem(esp_tmp,
1245  size=s),
1246  myebp))
1247  e.append(m2_expr.ExprAff(myebp, esp_tmp))
1248  e.append(m2_expr.ExprAff(myesp,
1249  myesp - (a + m2_expr.ExprInt(s / 8, s))))
1250  return e, []
1251 
1252 
1253 def jmp(ir, instr, dst):
1254  e = []
1255  meip = mRIP[instr.mode]
1256  e.append(m2_expr.ExprAff(meip, dst)) # dst.zeroExtend(instr.mode)))
1257  e.append(m2_expr.ExprAff(ir.IRDst, dst)) # dst.zeroExtend(instr.mode)))
1258 
1259  if isinstance(dst, m2_expr.ExprMem):
1260  dst = meip
1261  return e, []
1262 
1263 
1264 def jmpf(ir, instr, a):
1265  e = []
1266  meip = mRIP[instr.mode]
1267  s = instr.mode
1268  if (isinstance(a, m2_expr.ExprOp) and a.op == "segm"):
1269  segm = a.args[0]
1270  base = a.args[1]
1271  m1 = segm.zeroExtend(CS.size)#m2_expr.ExprMem(m2_expr.ExprOp('segm', segm, base), 16)
1272  m2 = base.zeroExtend(meip.size)#m2_expr.ExprMem(m2_expr.ExprOp('segm', segm, base + m2_expr.ExprInt_from(base, 2)), s)
1273  else:
1274  m1 = m2_expr.ExprMem(a, 16)
1275  m2 = m2_expr.ExprMem(a + m2_expr.ExprInt_from(a, 2), meip.size)
1276 
1277  e.append(m2_expr.ExprAff(CS, m1))
1278  e.append(m2_expr.ExprAff(meip, m2))
1279  e.append(m2_expr.ExprAff(ir.IRDst, m2))
1280  return e, []
1281 
1282 
1283 def jz(ir, instr, dst):
1284  return gen_jcc(ir, instr, zf, dst, True)
1285 
1286 
1287 def jcxz(ir, instr, dst):
1288  return gen_jcc(ir, instr, mRCX[instr.mode][:16], dst, False)
1289 
1290 
1291 def jecxz(ir, instr, dst):
1292  return gen_jcc(ir, instr, mRCX[instr.mode][:32], dst, False)
1293 
1294 
1295 def jrcxz(ir, instr, dst):
1296  return gen_jcc(ir, instr, mRCX[instr.mode], dst, False)
1297 
1298 
1299 def jnz(ir, instr, dst):
1300  return gen_jcc(ir, instr, zf, dst, False)
1301 
1302 
1303 def jp(ir, instr, dst):
1304  return gen_jcc(ir, instr, pf, dst, True)
1305 
1306 
1307 def jnp(ir, instr, dst):
1308  return gen_jcc(ir, instr, pf, dst, False)
1309 
1310 
1311 def ja(ir, instr, dst):
1312  return gen_jcc(ir, instr, cf|zf, dst, False)
1313 
1314 
1315 def jae(ir, instr, dst):
1316  return gen_jcc(ir, instr, cf, dst, False)
1317 
1318 
1319 def jb(ir, instr, dst):
1320  return gen_jcc(ir, instr, cf, dst, True)
1321 
1322 
1323 def jbe(ir, instr, dst):
1324  return gen_jcc(ir, instr, cf|zf, dst, True)
1325 
1326 
1327 def jge(ir, instr, dst):
1328  return gen_jcc(ir, instr, nf-of, dst, False)
1329 
1330 
1331 def jg(ir, instr, dst):
1332  return gen_jcc(ir, instr, zf|(nf-of), dst, False)
1333 
1334 
1335 def jl(ir, instr, dst):
1336  return gen_jcc(ir, instr, nf-of, dst, True)
1337 
1338 
1339 def jle(ir, instr, dst):
1340  return gen_jcc(ir, instr, zf|(nf-of), dst, True)
1341 
1342 
1343 def js(ir, instr, dst):
1344  return gen_jcc(ir, instr, nf, dst, True)
1345 
1346 
1347 def jns(ir, instr, dst):
1348  return gen_jcc(ir, instr, nf, dst, False)
1349 
1350 
1351 def jo(ir, instr, dst):
1352  return gen_jcc(ir, instr, of, dst, True)
1353 
1354 
1355 def jno(ir, instr, dst):
1356  return gen_jcc(ir, instr, of, dst, False)
1357 
1358 
1359 def loop(ir, instr, dst):
1360  e = []
1361  meip = mRIP[instr.mode]
1362  s = instr.v_opmode()
1363  opmode, admode = s, instr.v_admode()
1364  myecx = mRCX[instr.mode][:admode]
1365 
1366  n = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
1367  c = myecx - m2_expr.ExprInt_from(myecx, 1)
1368  dst_o = m2_expr.ExprCond(c,
1369  dst.zeroExtend(instr.mode),
1370  n.zeroExtend(instr.mode))
1371  e.append(m2_expr.ExprAff(myecx, c))
1372  e.append(m2_expr.ExprAff(meip, dst_o))
1373  e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
1374  return e, []
1375 
1376 
1377 def loopne(ir, instr, dst):
1378  e = []
1379  meip = mRIP[instr.mode]
1380  s = instr.v_opmode()
1381  opmode, admode = s, instr.v_admode()
1382  myecx = mRCX[instr.mode][:admode]
1383 
1384  n = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
1385 
1386  c = m2_expr.ExprCond(mRCX[instr.mode][:s] - m2_expr.ExprInt(1, s),
1387  m2_expr.ExprInt1(1),
1388  m2_expr.ExprInt1(0))
1389  c &= zf ^ m2_expr.ExprInt1(1)
1390 
1391  e.append(m2_expr.ExprAff(myecx, myecx - m2_expr.ExprInt_from(myecx, 1)))
1392  dst_o = m2_expr.ExprCond(c,
1393  dst.zeroExtend(instr.mode),
1394  n.zeroExtend(instr.mode))
1395  e.append(m2_expr.ExprAff(meip, dst_o))
1396  e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
1397  return e, []
1398 
1399 
1400 def loope(ir, instr, dst):
1401  e = []
1402  meip = mRIP[instr.mode]
1403  s = instr.v_opmode()
1404  opmode, admode = s, instr.v_admode()
1405  myecx = mRCX[instr.mode][:admode]
1406 
1407  n = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
1408  c = m2_expr.ExprCond(mRCX[instr.mode][:s] - m2_expr.ExprInt(1, s),
1409  m2_expr.ExprInt1(1),
1410  m2_expr.ExprInt1(0))
1411  c &= zf
1412  e.append(m2_expr.ExprAff(myecx, myecx - m2_expr.ExprInt_from(myecx, 1)))
1413  dst_o = m2_expr.ExprCond(c,
1414  dst.zeroExtend(instr.mode),
1415  n.zeroExtend(instr.mode))
1416  e.append(m2_expr.ExprAff(meip, dst_o))
1417  e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
1418  return e, []
1419 
1420 
1421 # XXX size to do; eflag
1422 def div(ir, instr, a):
1423  e = []
1424  size = a.size
1425  if size == 8:
1426  b = mRAX[instr.mode][:16]
1427  elif size in [16, 32, 64]:
1428  s1, s2 = mRDX[size], mRAX[size]
1429  b = m2_expr.ExprCompose([(s2, 0, size),
1430  (s1, size, size*2)])
1431  else:
1432  raise ValueError('div arg not impl', a)
1433 
1434  c_d = m2_expr.ExprOp('udiv', b, a.zeroExtend(b.size))
1435  c_r = m2_expr.ExprOp('umod', b, a.zeroExtend(b.size))
1436 
1437  # if 8 bit div, only ax is affected
1438  if size == 8:
1439  e.append(m2_expr.ExprAff(b, m2_expr.ExprCompose([(c_d[:8], 0, 8),
1440  (c_r[:8], 8, 16)])))
1441  else:
1442  e.append(m2_expr.ExprAff(s1, c_r[:size]))
1443  e.append(m2_expr.ExprAff(s2, c_d[:size]))
1444  return e, []
1445 
1446 # XXX size to do; eflag
1447 
1448 
1449 def idiv(ir, instr, a):
1450  e = []
1451  size = a.size
1452 
1453  if size == 8:
1454  b = mRAX[instr.mode][:16]
1455  elif size in [16, 32]:
1456  s1, s2 = mRDX[size], mRAX[size]
1457  b = m2_expr.ExprCompose([(s2, 0, size),
1458  (s1, size, size*2)])
1459  else:
1460  raise ValueError('div arg not impl', a)
1461 
1462  c_d = m2_expr.ExprOp('idiv', b, a.signExtend(b.size))
1463  c_r = m2_expr.ExprOp('imod', b, a.signExtend(b.size))
1464 
1465  # if 8 bit div, only ax is affected
1466  if size == 8:
1467  e.append(m2_expr.ExprAff(b, m2_expr.ExprCompose([(c_d[:8], 0, 8),
1468  (c_r[:8], 8, 16)])))
1469  else:
1470  e.append(m2_expr.ExprAff(s1, c_r[:size]))
1471  e.append(m2_expr.ExprAff(s2, c_d[:size]))
1472  return e, []
1473 
1474 # XXX size to do; eflag
1475 
1476 
1477 def mul(ir, instr, a):
1478  e = []
1479  size = a.size
1480  if a.size in [16, 32, 64]:
1481  result = m2_expr.ExprOp('*',
1482  mRAX[size].zeroExtend(size * 2),
1483  a.zeroExtend(size * 2))
1484  e.append(m2_expr.ExprAff(mRAX[size], result[:size]))
1485  e.append(m2_expr.ExprAff(mRDX[size], result[size:size * 2]))
1486 
1487  elif a.size == 8:
1488  result = m2_expr.ExprOp('*',
1489  mRAX[instr.mode][:8].zeroExtend(16),
1490  a.zeroExtend(16))
1491  e.append(m2_expr.ExprAff(mRAX[instr.mode][:16], result))
1492  else:
1493  raise ValueError('unknow size')
1494 
1495  e.append(m2_expr.ExprAff(of, m2_expr.ExprCond(result[size:size * 2],
1496  m2_expr.ExprInt1(1),
1497  m2_expr.ExprInt1(0))))
1498  e.append(m2_expr.ExprAff(cf, m2_expr.ExprCond(result[size:size * 2],
1499  m2_expr.ExprInt1(1),
1500  m2_expr.ExprInt1(0))))
1501 
1502  return e, []
1503 
1504 
1505 def imul(ir, instr, a, b=None, c=None):
1506  e = []
1507  size = a.size
1508  if b is None:
1509  if size in [16, 32, 64]:
1510  result = m2_expr.ExprOp('*',
1511  mRAX[size].signExtend(size * 2),
1512  a.signExtend(size * 2))
1513  e.append(m2_expr.ExprAff(mRAX[size], result[:size]))
1514  e.append(m2_expr.ExprAff(mRDX[size], result[size:size * 2]))
1515  elif size == 8:
1516  dst = mRAX[instr.mode][:16]
1517  result = m2_expr.ExprOp('*',
1518  mRAX[instr.mode][:8].signExtend(16),
1519  a.signExtend(16))
1520 
1521  e.append(m2_expr.ExprAff(dst, result))
1522  value = m2_expr.ExprCond(result - result[:size].signExtend(size * 2),
1523  m2_expr.ExprInt1(1),
1524  m2_expr.ExprInt1(0))
1525  e.append(m2_expr.ExprAff(cf, value))
1526  value = m2_expr.ExprCond(result - result[:size].signExtend(size * 2),
1527  m2_expr.ExprInt1(1),
1528  m2_expr.ExprInt1(0))
1529  e.append(m2_expr.ExprAff(of, value))
1530 
1531  else:
1532  if c is None:
1533  c = b
1534  b = a
1535  result = m2_expr.ExprOp('*',
1536  b.signExtend(size * 2),
1537  c.signExtend(size * 2))
1538  e.append(m2_expr.ExprAff(a, result[:size]))
1539 
1540  value = m2_expr.ExprCond(result - result[:size].signExtend(size * 2),
1541  m2_expr.ExprInt1(1),
1542  m2_expr.ExprInt1(0))
1543  e.append(m2_expr.ExprAff(cf, value))
1544  value = m2_expr.ExprCond(result - result[:size].signExtend(size * 2),
1545  m2_expr.ExprInt1(1),
1546  m2_expr.ExprInt1(0))
1547  e.append(m2_expr.ExprAff(of, value))
1548  return e, []
1549 
1550 
1551 def cbw(ir, instr):
1552  e = []
1553  tempAL = mRAX[instr.mode][:8]
1554  tempAX = mRAX[instr.mode][:16]
1555  e.append(m2_expr.ExprAff(tempAX, tempAL.signExtend(16)))
1556  return e, []
1557 
1558 
1559 def cwde(ir, instr):
1560  e = []
1561  tempAX = mRAX[instr.mode][:16]
1562  tempEAX = mRAX[instr.mode][:32]
1563  e.append(m2_expr.ExprAff(tempEAX, tempAX.signExtend(32)))
1564  return e, []
1565 
1566 
1567 def cdqe(ir, instr):
1568  e = []
1569  tempEAX = mRAX[instr.mode][:32]
1570  tempRAX = mRAX[instr.mode][:64]
1571  e.append(m2_expr.ExprAff(tempRAX, tempEAX.signExtend(64)))
1572  return e, []
1573 
1574 
1575 def cwd(ir, instr):
1576  e = []
1577  tempAX = mRAX[instr.mode][:16]
1578  tempDX = mRDX[instr.mode][:16]
1579  c = tempAX.signExtend(32)
1580  e.append(m2_expr.ExprAff(tempAX, c[:16]))
1581  e.append(m2_expr.ExprAff(tempDX, c[16:32]))
1582  return e, []
1583 
1584 
1585 def cdq(ir, instr):
1586  e = []
1587  tempEAX = mRAX[instr.mode][:32]
1588  tempEDX = mRDX[instr.mode][:32]
1589  c = tempEAX.signExtend(64)
1590  e.append(m2_expr.ExprAff(tempEAX, c[:32]))
1591  e.append(m2_expr.ExprAff(tempEDX, c[32:64]))
1592  return e, []
1593 
1594 
1595 def cqo(ir, instr):
1596  e = []
1597  tempRAX = mRAX[instr.mode][:64]
1598  tempRDX = mRDX[instr.mode][:64]
1599  c = tempRAX.signExtend(128)
1600  e.append(m2_expr.ExprAff(tempRAX, c[:64]))
1601  e.append(m2_expr.ExprAff(tempRDX, c[64:128]))
1602  return e, []
1603 
1604 
1605 def stos(ir, instr, size):
1606  lbl_df_0 = m2_expr.ExprId(ir.gen_label(), instr.mode)
1607  lbl_df_1 = m2_expr.ExprId(ir.gen_label(), instr.mode)
1608  lbl_next = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
1609 
1610  s = instr.v_admode()
1611 
1612  addr_o = mRDI[instr.mode][:s]
1613  addr = addr_o
1614  addr_p = addr + m2_expr.ExprInt_from(addr, size / 8)
1615  addr_m = addr - m2_expr.ExprInt_from(addr, size / 8)
1616  if ir.do_str_segm:
1617  mss = ES
1618  if instr.additional_info.g2.value:
1619  raise NotImplementedError("add segm support")
1620  addr = m2_expr.ExprOp('segm', mss, addr)
1621 
1622  b = mRAX[instr.mode][:size]
1623 
1624  e0 = []
1625  e0.append(m2_expr.ExprAff(addr_o, addr_p))
1626  e0.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
1627  e0 = irbloc(lbl_df_0.name, [e0])
1628 
1629  e1 = []
1630  e1.append(m2_expr.ExprAff(addr_o, addr_m))
1631  e1.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
1632  e1 = irbloc(lbl_df_1.name, [e1])
1633 
1634  e = []
1635  e.append(m2_expr.ExprAff(m2_expr.ExprMem(addr, size), b))
1636  e.append(m2_expr.ExprAff(ir.IRDst,
1637  m2_expr.ExprCond(df, lbl_df_1, lbl_df_0)))
1638  return e, [e0, e1]
1639 
1640 
1641 def lods(ir, instr, size):
1642  lbl_df_0 = m2_expr.ExprId(ir.gen_label(), instr.mode)
1643  lbl_df_1 = m2_expr.ExprId(ir.gen_label(), instr.mode)
1644  lbl_next = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
1645  e = []
1646  s = instr.v_admode()
1647 
1648  addr_o = mRSI[instr.mode][:s]
1649  addr = addr_o
1650  addr_p = addr + m2_expr.ExprInt_from(addr, size / 8)
1651  addr_m = addr - m2_expr.ExprInt_from(addr, size / 8)
1652  if ir.do_str_segm:
1653  mss = DS
1654  if instr.additional_info.g2.value:
1655  raise NotImplementedError("add segm support")
1656  addr = m2_expr.ExprOp('segm', mss, addr)
1657 
1658  b = mRAX[instr.mode][:size]
1659 
1660  e0 = []
1661  e0.append(m2_expr.ExprAff(addr_o, addr_p))
1662  e0.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
1663  e0 = irbloc(lbl_df_0.name, [e0])
1664 
1665  e1 = []
1666  e1.append(m2_expr.ExprAff(addr_o, addr_m))
1667  e1.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
1668  e1 = irbloc(lbl_df_1.name, [e1])
1669 
1670  e = []
1671  e.append(m2_expr.ExprAff(b, m2_expr.ExprMem(addr, size)))
1672 
1673  e.append(m2_expr.ExprAff(ir.IRDst,
1674  m2_expr.ExprCond(df, lbl_df_1, lbl_df_0)))
1675  return e, [e0, e1]
1676 
1677 
1678 def movs(ir, instr, size):
1679  lbl_df_0 = m2_expr.ExprId(ir.gen_label(), instr.mode)
1680  lbl_df_1 = m2_expr.ExprId(ir.gen_label(), instr.mode)
1681  lbl_next = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
1682 
1683  s = instr.v_admode()
1684  # a = m2_expr.ExprMem(mRDI[instr.mode][:s], size)
1685  # b = m2_expr.ExprMem(mRSI[instr.mode][:s], size)
1686 
1687  a = mRDI[instr.mode][:s]
1688  b = mRSI[instr.mode][:s]
1689 
1690  e = []
1691  src = b
1692  dst = a
1693  if ir.do_str_segm:
1694  if instr.additional_info.g2.value:
1695  raise NotImplementedError("add segm support")
1696  src = m2_expr.ExprOp('segm', DS, src)
1697  dst = m2_expr.ExprOp('segm', ES, dst)
1698  e.append(m2_expr.ExprAff(m2_expr.ExprMem(dst, size),
1699  m2_expr.ExprMem(src, size)))
1700 
1701  e0 = []
1702  e0.append(m2_expr.ExprAff(a, a + m2_expr.ExprInt_from(a, size / 8)))
1703  e0.append(m2_expr.ExprAff(b, b + m2_expr.ExprInt_from(b, size / 8)))
1704  e0.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
1705  e0 = irbloc(lbl_df_0.name, [e0])
1706 
1707  e1 = []
1708  e1.append(m2_expr.ExprAff(a, a - m2_expr.ExprInt_from(a, size / 8)))
1709  e1.append(m2_expr.ExprAff(b, b - m2_expr.ExprInt_from(b, size / 8)))
1710  e1.append(m2_expr.ExprAff(ir.IRDst, lbl_next))
1711  e1 = irbloc(lbl_df_1.name, [e1])
1712 
1713  e.append(m2_expr.ExprAff(ir.IRDst,
1714  m2_expr.ExprCond(df, lbl_df_1, lbl_df_0)))
1715  return e, [e0, e1]
1716 
1717 def movsd(ir, instr, a, b):
1718  e = []
1719  if isinstance(a, m2_expr.ExprId) and isinstance(b, m2_expr.ExprMem):
1720  b = m2_expr.ExprMem(b.arg, a.size)
1721  elif isinstance(a, m2_expr.ExprMem) and isinstance(b, m2_expr.ExprId):
1722  a = m2_expr.ExprMem(a.arg, b.size)
1723 
1724  e.append(m2_expr.ExprAff(a, b))
1725  return e, []
1726 
1727 def movsd_dispatch(ir, instr, a = None, b = None):
1728  if a is None and b is None:
1729  return movs(ir, instr, 32)
1730  else:
1731  return movsd(ir, instr, a, b)
1732 
1733 
1734 def float_prev(flt, popcount=1):
1735  if not flt in float_list:
1736  return None
1737  i = float_list.index(flt)
1738  if i < popcount:
1739  raise ValueError('broken index')
1740  flt = float_list[i - popcount]
1741  return flt
1742 
1743 
1744 def float_pop(avoid_flt=None, popcount=1):
1745  """
1746  Generate floatpop semantic (@popcount times), avoiding the avoid_flt@ float
1747  @avoid_flt: float avoided in the generated semantic
1748  @popcount: pop count
1749  """
1750  avoid_flt = float_prev(avoid_flt, popcount)
1751  e = []
1752  for i in xrange(8-popcount):
1753  if avoid_flt != float_list[i]:
1754  e.append(m2_expr.ExprAff(float_list[i],
1755  float_list[i+popcount]))
1756  for i in xrange(8-popcount, 8):
1757  e.append(m2_expr.ExprAff(float_list[i],
1758  m2_expr.ExprInt_from(float_list[i], 0)))
1759  e.append(
1760  m2_expr.ExprAff(float_stack_ptr,
1761  float_stack_ptr - m2_expr.ExprInt(popcount, 3)))
1762  return e
1763 
1764 # XXX TODO
1765 
1766 
1767 def fcom(ir, instr, a=None, b=None):
1768 
1769  if a is None and b is None:
1770  a, b = float_st0, float_st1
1771  elif b is None:
1772  b = a
1773  a = float_st0
1774 
1775  e = []
1776  b = mem2double(b)
1777 
1778  e.append(m2_expr.ExprAff(float_c0, m2_expr.ExprOp('fcom_c0', a, b)))
1779  e.append(m2_expr.ExprAff(float_c1, m2_expr.ExprOp('fcom_c1', a, b)))
1780  e.append(m2_expr.ExprAff(float_c2, m2_expr.ExprOp('fcom_c2', a, b)))
1781  e.append(m2_expr.ExprAff(float_c3, m2_expr.ExprOp('fcom_c3', a, b)))
1782 
1783  e += set_float_cs_eip(instr)
1784  return e, []
1785 
1786 
1787 def ftst(ir, instr):
1788  a = float_st0
1789 
1790  e = []
1791  b = m2_expr.ExprOp('int_32_to_double', m2_expr.ExprInt32(0))
1792  e.append(m2_expr.ExprAff(float_c0, m2_expr.ExprOp('fcom_c0', a, b)))
1793  e.append(m2_expr.ExprAff(float_c1, m2_expr.ExprOp('fcom_c1', a, b)))
1794  e.append(m2_expr.ExprAff(float_c2, m2_expr.ExprOp('fcom_c2', a, b)))
1795  e.append(m2_expr.ExprAff(float_c3, m2_expr.ExprOp('fcom_c3', a, b)))
1796 
1797  e += set_float_cs_eip(instr)
1798  return e, []
1799 
1800 
1801 def fxam(ir, instr):
1802  a = float_st0
1803 
1804  e = []
1805  e.append(m2_expr.ExprAff(float_c0, m2_expr.ExprOp('fxam_c0', a)))
1806  e.append(m2_expr.ExprAff(float_c2, m2_expr.ExprOp('fxam_c2', a)))
1807  e.append(m2_expr.ExprAff(float_c3, m2_expr.ExprOp('fxam_c3', a)))
1808 
1809  e += set_float_cs_eip(instr)
1810  return e, []
1811 
1812 
1813 def ficom(ir, instr, a, b = None):
1814 
1815  a, b = float_implicit_st0(a, b)
1816 
1817  e = []
1818 
1819  e.append(m2_expr.ExprAff(float_c0,
1820  m2_expr.ExprOp('fcom_c0', a,
1821  b.zeroExtend(a.size))))
1822  e.append(m2_expr.ExprAff(float_c1,
1823  m2_expr.ExprOp('fcom_c1', a,
1824  b.zeroExtend(a.size))))
1825  e.append(m2_expr.ExprAff(float_c2,
1826  m2_expr.ExprOp('fcom_c2', a,
1827  b.zeroExtend(a.size))))
1828  e.append(m2_expr.ExprAff(float_c3,
1829  m2_expr.ExprOp('fcom_c3', a,
1830  b.zeroExtend(a.size))))
1831 
1832  e += set_float_cs_eip(instr)
1833  return e, []
1834 
1835 
1836 
1837 def fcomi(ir, instr, a=None, b=None):
1838  # TODO unordered float
1839  if a is None and b is None:
1840  a, b = float_st0, float_st1
1841  elif b is None:
1842  b = a
1843  a = float_st0
1844 
1845  e = []
1846 
1847  e.append(m2_expr.ExprAff(cf, m2_expr.ExprOp('fcom_c0', a, b)))
1848  e.append(m2_expr.ExprAff(pf, m2_expr.ExprOp('fcom_c2', a, b)))
1849  e.append(m2_expr.ExprAff(zf, m2_expr.ExprOp('fcom_c3', a, b)))
1850 
1851  e.append(m2_expr.ExprAff(of, m2_expr.ExprInt1(0)))
1852  e.append(m2_expr.ExprAff(nf, m2_expr.ExprInt1(0)))
1853  e.append(m2_expr.ExprAff(af, m2_expr.ExprInt1(0)))
1854 
1855  e += set_float_cs_eip(instr)
1856  return e, []
1857 
1858 
1859 def fcomip(ir, instr, a=None, b=None):
1860  e, extra = fcomi(ir, instr, a, b)
1861  e += float_pop()
1862  e += set_float_cs_eip(instr)
1863  return e, extra
1864 
1865 
1866 def fucomi(ir, instr, a=None, b=None):
1867  # TODO unordered float
1868  return fcomi(ir, instr, a, b)
1869 
1870 def fucomip(ir, instr, a=None, b=None):
1871  # TODO unordered float
1872  return fcomip(ir, instr, a, b)
1873 
1874 
1875 def fcomp(ir, instr, a=None, b=None):
1876  e, extra = fcom(ir, instr, a, b)
1877  e += float_pop()
1878  e += set_float_cs_eip(instr)
1879  return e, extra
1880 
1881 
1882 def fcompp(ir, instr, a=None, b=None):
1883  e, extra = fcom(ir, instr, a, b)
1884  e += float_pop(popcount=2)
1885  e += set_float_cs_eip(instr)
1886  return e, extra
1887 
1888 
1889 def ficomp(ir, instr, a, b = None):
1890  e, extra = ficom(ir, instr, a, b)
1891  e += float_pop()
1892  e += set_float_cs_eip(instr)
1893  return e, extra
1894 
1895 
1896 def fucom(ir, instr, a=None, b=None):
1897  # TODO unordered float
1898  return fcom(ir, instr, a, b)
1899 
1900 
1901 def fucomp(ir, instr, a=None, b=None):
1902  # TODO unordered float
1903  return fcomp(ir, instr, a, b)
1904 
1905 
1906 def fucompp(ir, instr, a=None, b=None):
1907  # TODO unordered float
1908  return fcompp(ir, instr, a, b)
1909 
1910 
1911 def comiss(ir, instr, a, b):
1912  # TODO unordered float
1913 
1914  e = []
1915 
1916  a = m2_expr.ExprOp('int_32_to_float', a[:32])
1917  b = m2_expr.ExprOp('int_32_to_float', b[:32])
1918 
1919  e.append(m2_expr.ExprAff(cf, m2_expr.ExprOp('fcom_c0', a, b)))
1920  e.append(m2_expr.ExprAff(pf, m2_expr.ExprOp('fcom_c2', a, b)))
1921  e.append(m2_expr.ExprAff(zf, m2_expr.ExprOp('fcom_c3', a, b)))
1922 
1923  e.append(m2_expr.ExprAff(of, m2_expr.ExprInt1(0)))
1924  e.append(m2_expr.ExprAff(nf, m2_expr.ExprInt1(0)))
1925  e.append(m2_expr.ExprAff(af, m2_expr.ExprInt1(0)))
1926 
1927  e += set_float_cs_eip(instr)
1928  return e, []
1929 
1930 
1931 def comisd(ir, instr, a, b):
1932  # TODO unordered float
1933 
1934  e = []
1935 
1936  a = m2_expr.ExprOp('int_64_to_double', a[:64])
1937  b = m2_expr.ExprOp('int_64_to_double', b[:64])
1938 
1939  e.append(m2_expr.ExprAff(cf, m2_expr.ExprOp('fcom_c0', a, b)))
1940  e.append(m2_expr.ExprAff(pf, m2_expr.ExprOp('fcom_c2', a, b)))
1941  e.append(m2_expr.ExprAff(zf, m2_expr.ExprOp('fcom_c3', a, b)))
1942 
1943  e.append(m2_expr.ExprAff(of, m2_expr.ExprInt1(0)))
1944  e.append(m2_expr.ExprAff(nf, m2_expr.ExprInt1(0)))
1945  e.append(m2_expr.ExprAff(af, m2_expr.ExprInt1(0)))
1946 
1947  e += set_float_cs_eip(instr)
1948  return e, []
1949 
1950 
1951 def fld(ir, instr, a):
1952  src = mem2double(a)
1953 
1954  e = []
1955  e.append(m2_expr.ExprAff(float_st7, float_st6))
1956  e.append(m2_expr.ExprAff(float_st6, float_st5))
1957  e.append(m2_expr.ExprAff(float_st5, float_st4))
1958  e.append(m2_expr.ExprAff(float_st4, float_st3))
1959  e.append(m2_expr.ExprAff(float_st3, float_st2))
1960  e.append(m2_expr.ExprAff(float_st2, float_st1))
1961  e.append(m2_expr.ExprAff(float_st1, float_st0))
1962  e.append(m2_expr.ExprAff(float_st0, src))
1963  e.append(
1964  m2_expr.ExprAff(float_stack_ptr,
1965  float_stack_ptr + m2_expr.ExprInt(1, 3)))
1966 
1967  e += set_float_cs_eip(instr)
1968  return e, []
1969 
1970 
1971 def fst(ir, instr, a):
1972  e = []
1973 
1974  if isinstance(a, m2_expr.ExprMem):
1975  if a.size > 64:
1976  raise NotImplementedError('float to long')
1977  src = m2_expr.ExprOp('double_to_mem_%.2d' % a.size, a)
1978  else:
1979  src = a
1980 
1981  e.append(m2_expr.ExprAff(a, src))
1982  e += set_float_cs_eip(instr)
1983  return e, []
1984 
1985 
1986 def fstp(ir, instr, a):
1987  e, extra = fst(ir, instr, a)
1988  e += float_pop(a)
1989  return e, extra
1990 
1991 
1992 def fist(ir, instr, a):
1993  e = []
1994  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('double_to_int_%d' % a.size,
1995  float_st0)))
1996 
1997  e += set_float_cs_eip(instr)
1998  return e, []
1999 
2000 def fistp(ir, instr, a):
2001  e, extra = fist(ir, instr, a)
2002  e += float_pop(a)
2003  return e, extra
2004 
2005 def fist(ir, instr, a):
2006  e = []
2007  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('double_to_int_%d' % a.size,
2008  float_st0)))
2009 
2010  e += set_float_cs_eip(instr)
2011  return e, []
2012 
2013 def fisttp(ir, instr, a):
2014  e = []
2015  e.append(m2_expr.ExprAff(a,
2016  m2_expr.ExprOp('double_trunc_to_int_%d' % a.size,
2017  float_st0)))
2018 
2019  e += set_float_cs_eip(instr)
2020  e += float_pop(a)
2021  return e, []
2022 
2023 
2024 def fild(ir, instr, a):
2025  # XXXXX
2026  src = m2_expr.ExprOp('int_%.2d_to_double' % a.size, a)
2027  e = []
2028  e += set_float_cs_eip(instr)
2029  e_fld, extra = fld(ir, instr, src)
2030  e += e_fld
2031  return e, extra
2032 
2033 
2034 def fldz(ir, instr):
2035  return fld(ir, instr, m2_expr.ExprOp('int_32_to_double',
2036  m2_expr.ExprInt32(0)))
2037 
2038 
2039 def fld1(ir, instr):
2040  return fld(ir, instr, m2_expr.ExprOp('int_32_to_double',
2041  m2_expr.ExprInt32(1)))
2042 
2043 
2044 def fldl2t(ir, instr):
2045  value_f = math.log(10)/math.log(2)
2046  value = struct.unpack('I', struct.pack('f', value_f))[0]
2047  return fld(ir, instr, m2_expr.ExprOp('int_32_to_double',
2048  m2_expr.ExprInt32(value)))
2049 
2050 
2051 def fldpi(ir, instr):
2052  value_f = math.pi
2053  value = struct.unpack('I', struct.pack('f', value_f))[0]
2054  return fld(ir, instr, m2_expr.ExprOp('int_32_to_double',
2055  m2_expr.ExprInt32(value)))
2056 
2057 
2058 def fldln2(ir, instr):
2059  value_f = math.log(2)
2060  value = struct.unpack('I', struct.pack('f', value_f))[0]
2061  return fld(ir, instr, m2_expr.ExprOp('int_32_to_double',
2062  m2_expr.ExprInt32(value)))
2063 
2064 
2065 def fldl2e(ir, instr):
2066  x = struct.pack('d', 1 / math.log(2))
2067  x = struct.unpack('Q', x)[0]
2068  return fld(ir, instr, m2_expr.ExprOp('mem_64_to_double',
2069  m2_expr.ExprInt64(x)))
2070 
2071 
2072 def fldlg2(ir, instr):
2073  x = struct.pack('d', math.log10(2))
2074  x = struct.unpack('Q', x)[0]
2075  return fld(ir, instr, m2_expr.ExprOp('mem_64_to_double',
2076  m2_expr.ExprInt64(x)))
2077 
2078 
2079 def fadd(ir, instr, a, b=None):
2080  a, b = float_implicit_st0(a, b)
2081  e = []
2082  src = mem2double(b)
2083  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fadd', a, src)))
2084 
2085  e += set_float_cs_eip(instr)
2086  return e, []
2087 
2088 def fiadd(ir, instr, a, b=None):
2089  a, b = float_implicit_st0(a, b)
2090  e = []
2091  src = mem2double(b)
2092  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fiadd', a, src)))
2093  e += set_float_cs_eip(instr)
2094  return e, []
2095 
2096 
2097 def fisub(ir, instr, a, b=None):
2098  a, b = float_implicit_st0(a, b)
2099  e = []
2100  src = mem2double(b)
2101  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fisub', a, src)))
2102  e += set_float_cs_eip(instr)
2103  return e, []
2104 
2105 
2106 def fisubr(ir, instr, a, b=None):
2107  a, b = float_implicit_st0(a, b)
2108  e = []
2109  src = mem2double(b)
2110  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fisub', src, a)))
2111  e += set_float_cs_eip(instr)
2112  return e, []
2113 
2114 
2115 def fpatan(ir, instr):
2116  e = []
2117  a = float_st1
2118  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fpatan', float_st0, float_st1)))
2119  e += set_float_cs_eip(instr)
2120  e += float_pop(a)
2121  return e, []
2122 
2123 
2124 def fprem(ir, instr):
2125  e = []
2126  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fprem', float_st0, float_st1)))
2127  e += set_float_cs_eip(instr)
2128  return e, []
2129 
2130 
2131 def fprem1(ir, instr):
2132  e = []
2133  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fprem1', float_st0, float_st1)))
2134  e += set_float_cs_eip(instr)
2135  return e, []
2136 
2137 
2138 def faddp(ir, instr, a, b=None):
2139  a, b = float_implicit_st0(a, b)
2140  e = []
2141  src = mem2double(b)
2142  e.append(m2_expr.ExprAff(float_prev(a), m2_expr.ExprOp('fadd', a, src)))
2143  e += set_float_cs_eip(instr)
2144  e += float_pop(a)
2145  return e, []
2146 
2147 
2148 def fninit(ir, instr):
2149  e = []
2150  e += set_float_cs_eip(instr)
2151  return e, []
2152 
2153 
2154 def fyl2x(ir, instr):
2155  e = []
2156  a = float_st1
2157  e.append(m2_expr.ExprAff(float_prev(a), m2_expr.ExprOp('fyl2x', float_st0, float_st1)))
2158  e += set_float_cs_eip(instr)
2159  e += float_pop(a)
2160  return e, []
2161 
2162 
2163 def fnstenv(ir, instr, a):
2164  e = []
2165  # XXX TODO tag word, ...
2166  status_word = m2_expr.ExprCompose([(m2_expr.ExprInt8(0), 0, 8),
2167  (float_c0, 8, 9),
2168  (float_c1, 9, 10),
2169  (float_c2, 10, 11),
2170  (float_stack_ptr, 11, 14),
2171  (float_c3, 14, 15),
2172  (m2_expr.ExprInt1(0), 15, 16),
2173  ])
2174 
2175  s = instr.mode
2176  # The behaviour in 64bit is identical to 64 bit
2177  # This will truncate addresses
2178  s = min(32, s)
2179  ad = m2_expr.ExprMem(a.arg, size=16)
2180  e.append(m2_expr.ExprAff(ad, float_control))
2181  ad = m2_expr.ExprMem(a.arg + m2_expr.ExprInt_from(a.arg, s / 8 * 1),
2182  size=16)
2183  e.append(m2_expr.ExprAff(ad, status_word))
2184  ad = m2_expr.ExprMem(a.arg + m2_expr.ExprInt_from(a.arg, s / 8 * 3),
2185  size=s)
2186  e.append(m2_expr.ExprAff(ad, float_eip[:s]))
2187  ad = m2_expr.ExprMem(a.arg + m2_expr.ExprInt_from(a.arg, s / 8 * 4),
2188  size=16)
2189  e.append(m2_expr.ExprAff(ad, float_cs))
2190  ad = m2_expr.ExprMem(a.arg + m2_expr.ExprInt_from(a.arg, s / 8 * 5),
2191  size=s)
2192  e.append(m2_expr.ExprAff(ad, float_address[:s]))
2193  ad = m2_expr.ExprMem(a.arg + m2_expr.ExprInt_from(a.arg, s / 8 * 6),
2194  size=16)
2195  e.append(m2_expr.ExprAff(ad, float_ds))
2196  return e, []
2197 
2198 
2199 def fsub(ir, instr, a, b=None):
2200  a, b = float_implicit_st0(a, b)
2201  e = []
2202  src = mem2double(b)
2203  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fsub', a, src)))
2204  e += set_float_cs_eip(instr)
2205  return e, []
2206 
2207 def fsubp(ir, instr, a, b=None):
2208  a, b = float_implicit_st0(a, b)
2209  e = []
2210  src = mem2double(b)
2211  e.append(m2_expr.ExprAff(float_prev(a), m2_expr.ExprOp('fsub', a, src)))
2212  e += set_float_cs_eip(instr)
2213  e += float_pop(a)
2214  return e, []
2215 
2216 
2217 def fsubr(ir, instr, a, b=None):
2218  a, b = float_implicit_st0(a, b)
2219  e = []
2220  src = mem2double(b)
2221  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fsub', src, a)))
2222  e += set_float_cs_eip(instr)
2223  return e, []
2224 
2225 
2226 def fsubrp(ir, instr, a, b=None):
2227  a, b = float_implicit_st0(a, b)
2228  e = []
2229  src = mem2double(b)
2230  e.append(m2_expr.ExprAff(float_prev(a), m2_expr.ExprOp('fsub', src, a)))
2231  e += set_float_cs_eip(instr)
2232  e += float_pop(a)
2233  return e, []
2234 
2235 
2236 def fmul(ir, instr, a, b=None):
2237  a, b = float_implicit_st0(a, b)
2238  e = []
2239  src = mem2double(b)
2240  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fmul', a, src)))
2241  e += set_float_cs_eip(instr)
2242  return e, []
2243 
2244 def fimul(ir, instr, a, b=None):
2245  a, b = float_implicit_st0(a, b)
2246  e = []
2247  src = mem2double(b)
2248  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fimul', a, src)))
2249  e += set_float_cs_eip(instr)
2250  return e, []
2251 
2252 
2253 def fdiv(ir, instr, a, b=None):
2254  a, b = float_implicit_st0(a, b)
2255  e = []
2256  src = mem2double(b)
2257  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fdiv', a, src)))
2258  e += set_float_cs_eip(instr)
2259  return e, []
2260 
2261 def fdivr(ir, instr, a, b=None):
2262  a, b = float_implicit_st0(a, b)
2263  e = []
2264  src = mem2double(b)
2265  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fdiv', src, a)))
2266  e += set_float_cs_eip(instr)
2267  return e, []
2268 
2269 
2270 def fdivrp(ir, instr, a, b=None):
2271  a, b = float_implicit_st0(a, b)
2272  e = []
2273  src = mem2double(b)
2274  e.append(m2_expr.ExprAff(float_prev(a), m2_expr.ExprOp('fdiv', src, a)))
2275  e += set_float_cs_eip(instr)
2276  e += float_pop(a)
2277  return e, []
2278 
2279 
2280 def fidiv(ir, instr, a, b=None):
2281  a, b = float_implicit_st0(a, b)
2282  e = []
2283  src = mem2double(b)
2284  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fidiv', a, src)))
2285  e += set_float_cs_eip(instr)
2286  return e, []
2287 
2288 
2289 def fidivr(ir, instr, a, b=None):
2290  a, b = float_implicit_st0(a, b)
2291  e = []
2292  src = mem2double(b)
2293  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('fidiv', src, a)))
2294  e += set_float_cs_eip(instr)
2295  return e, []
2296 
2297 
2298 def fdivp(ir, instr, a, b=None):
2299  # Invalid emulation
2300  a, b = float_implicit_st0(a, b)
2301  e = []
2302  src = mem2double(b)
2303  e.append(m2_expr.ExprAff(float_prev(a), m2_expr.ExprOp('fdiv', a, src)))
2304  e += set_float_cs_eip(instr)
2305  e += float_pop(a)
2306  return e, []
2307 
2308 
2309 def fmulp(ir, instr, a, b=None):
2310  # Invalid emulation
2311  a, b = float_implicit_st0(a, b)
2312  e = []
2313  src = mem2double(b)
2314  e.append(m2_expr.ExprAff(float_prev(a), m2_expr.ExprOp('fmul', a, src)))
2315  e += set_float_cs_eip(instr)
2316  e += float_pop(a)
2317  return e, []
2318 
2319 
2320 def ftan(ir, instr, a):
2321  e = []
2322  src = mem2double(a)
2323  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('ftan', src)))
2324  e += set_float_cs_eip(instr)
2325  return e, []
2326 
2327 
2328 def fxch(ir, instr, a):
2329  e = []
2330  src = mem2double(a)
2331  e.append(m2_expr.ExprAff(float_st0, src))
2332  e.append(m2_expr.ExprAff(src, float_st0))
2333  e += set_float_cs_eip(instr)
2334  return e, []
2335 
2336 
2337 def fptan(ir, instr):
2338  e = []
2339  e.append(m2_expr.ExprAff(float_st7, float_st6))
2340  e.append(m2_expr.ExprAff(float_st6, float_st5))
2341  e.append(m2_expr.ExprAff(float_st5, float_st4))
2342  e.append(m2_expr.ExprAff(float_st4, float_st3))
2343  e.append(m2_expr.ExprAff(float_st3, float_st2))
2344  e.append(m2_expr.ExprAff(float_st2, float_st1))
2345  e.append(m2_expr.ExprAff(float_st1, m2_expr.ExprOp('ftan', float_st0)))
2346  e.append(m2_expr.ExprAff(float_st0,
2347  m2_expr.ExprOp('int_32_to_double',
2348  m2_expr.ExprInt32(1))))
2349  e.append(
2350  m2_expr.ExprAff(float_stack_ptr,
2351  float_stack_ptr + m2_expr.ExprInt(1, 3)))
2352  return e, []
2353 
2354 
2355 def frndint(ir, instr):
2356  e = []
2357  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('frndint', float_st0)))
2358  e += set_float_cs_eip(instr)
2359  return e, []
2360 
2361 
2362 def fsin(ir, instr):
2363  e = []
2364  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fsin', float_st0)))
2365  e += set_float_cs_eip(instr)
2366  return e, []
2367 
2368 
2369 def fcos(ir, instr):
2370  e = []
2371  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fcos', float_st0)))
2372  e += set_float_cs_eip(instr)
2373  return e, []
2374 
2375 
2376 def fsincos(ir, instr):
2377  e = []
2378  e.append(m2_expr.ExprAff(float_st7, float_st6))
2379  e.append(m2_expr.ExprAff(float_st6, float_st5))
2380  e.append(m2_expr.ExprAff(float_st5, float_st4))
2381  e.append(m2_expr.ExprAff(float_st4, float_st3))
2382  e.append(m2_expr.ExprAff(float_st3, float_st2))
2383  e.append(m2_expr.ExprAff(float_st2, float_st1))
2384  e.append(m2_expr.ExprAff(float_st1, m2_expr.ExprOp('fsin', float_st0)))
2385  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fcos', float_st0)))
2386  e.append(
2387  m2_expr.ExprAff(float_stack_ptr,
2388  float_stack_ptr + m2_expr.ExprInt(1, 3)))
2389  return e, []
2390 
2391 
2392 def fscale(ir, instr):
2393  e = []
2394  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fscale', float_st0,
2395  float_st1)))
2396  e += set_float_cs_eip(instr)
2397  return e, []
2398 
2399 
2400 def f2xm1(ir, instr):
2401  e = []
2402  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('f2xm1', float_st0)))
2403  e += set_float_cs_eip(instr)
2404  return e, []
2405 
2406 def fchs(ir, instr):
2407  e = []
2408  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fchs', float_st0)))
2409  e += set_float_cs_eip(instr)
2410  return e, []
2411 
2412 
2413 def fsqrt(ir, instr):
2414  e = []
2415  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fsqrt', float_st0)))
2416  e += set_float_cs_eip(instr)
2417  return e, []
2418 
2419 
2420 def fabs(ir, instr):
2421  e = []
2422  e.append(m2_expr.ExprAff(float_st0, m2_expr.ExprOp('fabs', float_st0)))
2423  e += set_float_cs_eip(instr)
2424  return e, []
2425 
2426 
2427 def fnstsw(ir, instr, dst):
2428  args = [(m2_expr.ExprInt8(0), 0, 8),
2429  (float_c0, 8, 9),
2430  (float_c1, 9, 10),
2431  (float_c2, 10, 11),
2432  (float_stack_ptr, 11, 14),
2433  (float_c3, 14, 15),
2434  (m2_expr.ExprInt1(0), 15, 16)]
2435  e = [m2_expr.ExprAff(dst, m2_expr.ExprCompose(args))]
2436  return e, []
2437 
2438 
2439 def fnstcw(ir, instr, a):
2440  e = []
2441  e.append(m2_expr.ExprAff(a, float_control))
2442  return e, []
2443 
2444 
2445 def fldcw(ir, instr, a):
2446  e = []
2447  e.append(m2_expr.ExprAff(float_control, a))
2448  return e, []
2449 
2450 
2451 def fwait(ir, instr):
2452  return [], None
2453 
2454 
2455 def fcmovb(ir, instr, arg1, arg2):
2456  return gen_fcmov(ir, instr, cf, arg1, arg2, True)
2457 
2458 
2459 def fcmove(ir, instr, arg1, arg2):
2460  return gen_fcmov(ir, instr, zf, arg1, arg2, True)
2461 
2462 
2463 def fcmovbe(ir, instr, arg1, arg2):
2464  return gen_fcmov(ir, instr, cf|zf, arg1, arg2, True)
2465 
2466 
2467 def fcmovu(ir, instr, arg1, arg2):
2468  return gen_fcmov(ir, instr, pf, arg1, arg2, True)
2469 
2470 
2471 def fcmovnb(ir, instr, arg1, arg2):
2472  return gen_fcmov(ir, instr, cf, arg1, arg2, False)
2473 
2474 
2475 def fcmovne(ir, instr, arg1, arg2):
2476  return gen_fcmov(ir, instr, zf, arg1, arg2, False)
2477 
2478 
2479 def fcmovnbe(ir, instr, arg1, arg2):
2480  return gen_fcmov(ir, instr, cf|zf, arg1, arg2, False)
2481 
2482 
2483 def fcmovnu(ir, instr, arg1, arg2):
2484  return gen_fcmov(ir, instr, pf, arg1, arg2, False)
2485 
2486 
2487 def nop(ir, instr, a=None):
2488  return [], []
2489 
2490 
2491 def hlt(ir, instr):
2492  e = []
2493  except_int = EXCEPT_PRIV_INSN
2494  e.append(m2_expr.ExprAff(exception_flags, m2_expr.ExprInt32(except_int)))
2495  return e, []
2496 
2497 
2498 def rdtsc(ir, instr):
2499  e = []
2500  e.append(m2_expr.ExprAff(tsc1, tsc1 + m2_expr.ExprInt32(1)))
2501  e.append(m2_expr.ExprAff(mRAX[32], tsc1))
2502  e.append(m2_expr.ExprAff(mRDX[32], tsc2))
2503  return e, []
2504 
2505 
2506 def daa(ir, instr):
2507  e = []
2508  r_al = mRAX[instr.mode][:8]
2509 
2510  cond1 = expr_cmpu(r_al[:4], m2_expr.ExprInt(0x9, 4)) | af
2511  e.append(m2_expr.ExprAff(af, cond1))
2512 
2513 
2514  cond2 = expr_cmpu(m2_expr.ExprInt8(6), r_al)
2515  cond3 = expr_cmpu(r_al, m2_expr.ExprInt8(0x99)) | cf
2516 
2517 
2518  cf_c1 = m2_expr.ExprCond(cond1,
2519  cf | (cond2),
2520  m2_expr.ExprInt1(0))
2521  new_cf = m2_expr.ExprCond(cond3,
2522  m2_expr.ExprInt1(1),
2523  m2_expr.ExprInt1(0))
2524  e.append(m2_expr.ExprAff(cf, new_cf))
2525 
2526  al_c1 = m2_expr.ExprCond(cond1,
2527  r_al + m2_expr.ExprInt8(6),
2528  r_al)
2529 
2530  new_al = m2_expr.ExprCond(cond3,
2531  al_c1 + m2_expr.ExprInt8(0x60),
2532  al_c1)
2533  e.append(m2_expr.ExprAff(r_al, new_al))
2534  return e, []
2535 
2536 def das(ir, instr):
2537  e = []
2538  r_al = mRAX[instr.mode][:8]
2539 
2540  cond1 = expr_cmpu(r_al[:4], m2_expr.ExprInt(0x9, 4)) | af
2541  e.append(m2_expr.ExprAff(af, cond1))
2542 
2543 
2544  cond2 = expr_cmpu(m2_expr.ExprInt8(6), r_al)
2545  cond3 = expr_cmpu(r_al, m2_expr.ExprInt8(0x99)) | cf
2546 
2547 
2548  cf_c1 = m2_expr.ExprCond(cond1,
2549  cf | (cond2),
2550  m2_expr.ExprInt1(0))
2551  new_cf = m2_expr.ExprCond(cond3,
2552  m2_expr.ExprInt1(1),
2553  cf_c1)
2554  e.append(m2_expr.ExprAff(cf, new_cf))
2555 
2556  al_c1 = m2_expr.ExprCond(cond1,
2557  r_al - m2_expr.ExprInt8(6),
2558  r_al)
2559 
2560  new_al = m2_expr.ExprCond(cond3,
2561  al_c1 - m2_expr.ExprInt8(0x60),
2562  al_c1)
2563  e.append(m2_expr.ExprAff(r_al, new_al))
2564  return e, []
2565 
2566 
2567 def aam(ir, instr, a):
2568  e = []
2569  tempAL = mRAX[instr.mode][0:8]
2570  newEAX = m2_expr.ExprCompose([
2571  (tempAL % a, 0, 8),
2572  (tempAL / a, 8, 16),
2573  (mRAX[instr.mode][16:], 16, mRAX[instr.mode].size),
2574  ])
2575  e += [m2_expr.ExprAff(mRAX[instr.mode], newEAX)]
2576  e += update_flag_arith(newEAX)
2577  return e, []
2578 
2579 
2580 def aad(ir, instr, a):
2581  e = []
2582  tempAL = mRAX[instr.mode][0:8]
2583  tempAH = mRAX[instr.mode][8:16]
2584  newEAX = m2_expr.ExprCompose([
2585  ((tempAL + (tempAH * a)) & m2_expr.ExprInt8(0xFF), 0, 8),
2586  (m2_expr.ExprInt8(0), 8, 16),
2587  (mRAX[instr.mode][16:],
2588  16, mRAX[instr.mode].size),
2589  ])
2590  e += [m2_expr.ExprAff(mRAX[instr.mode], newEAX)]
2591  e += update_flag_arith(newEAX)
2592  return e, []
2593 
2594 
2595 def aaa(ir, instr, ):
2596  e = []
2597  c = (mRAX[instr.mode][:8] & m2_expr.ExprInt8(0xf)) - m2_expr.ExprInt8(9)
2598 
2599  c = m2_expr.ExprCond(c.msb(),
2600  m2_expr.ExprInt1(0),
2601  m2_expr.ExprInt1(1)) & \
2602  m2_expr.ExprCond(c,
2603  m2_expr.ExprInt1(1),
2604  m2_expr.ExprInt1(0))
2605 
2606  c |= af & m2_expr.ExprInt1(1)
2607  # set AL
2608  m_al = m2_expr.ExprCond(c,
2609  (mRAX[instr.mode][:8] + m2_expr.ExprInt8(6)) & \
2610  m2_expr.ExprInt8(0xF),
2611  mRAX[instr.mode][:8] & m2_expr.ExprInt8(0xF))
2612  m_ah = m2_expr.ExprCond(c,
2613  mRAX[instr.mode][8:16] + m2_expr.ExprInt8(1),
2614  mRAX[instr.mode][8:16])
2615 
2616  e.append(m2_expr.ExprAff(mRAX[instr.mode], m2_expr.ExprCompose([
2617  (m_al, 0, 8), (m_ah, 8, 16),
2618  (mRAX[instr.mode][16:], 16, mRAX[instr.mode].size)])))
2619  e.append(m2_expr.ExprAff(af, c))
2620  e.append(m2_expr.ExprAff(cf, c))
2621  return e, []
2622 
2623 
2624 def aas(ir, instr, ):
2625  e = []
2626  c = (mRAX[instr.mode][:8] & m2_expr.ExprInt8(0xf)) - m2_expr.ExprInt8(9)
2627 
2628  c = m2_expr.ExprCond(c.msb(),
2629  m2_expr.ExprInt1(0),
2630  m2_expr.ExprInt1(1)) & \
2631  m2_expr.ExprCond(c,
2632  m2_expr.ExprInt1(1),
2633  m2_expr.ExprInt1(0))
2634 
2635  c |= af & m2_expr.ExprInt1(1)
2636  # set AL
2637  m_al = m2_expr.ExprCond(c,
2638  (mRAX[instr.mode][:8] - m2_expr.ExprInt8(6)) & \
2639  m2_expr.ExprInt8(0xF),
2640  mRAX[instr.mode][:8] & m2_expr.ExprInt8(0xF))
2641  m_ah = m2_expr.ExprCond(c,
2642  mRAX[instr.mode][8:16] - m2_expr.ExprInt8(1),
2643  mRAX[instr.mode][8:16])
2644 
2645  e.append(m2_expr.ExprAff(mRAX[instr.mode], m2_expr.ExprCompose([
2646  (m_al, 0, 8), (m_ah, 8, 16),
2647  (mRAX[instr.mode][16:], 16, mRAX[instr.mode].size)])))
2648  e.append(m2_expr.ExprAff(af, c))
2649  e.append(m2_expr.ExprAff(cf, c))
2650  return e, []
2651 
2652 
2653 def bsr_bsf(ir, instr, a, b, op_name):
2654  """
2655  IF SRC == 0
2656  ZF = 1
2657  DEST is left unchanged
2658  ELSE
2659  ZF = 0
2660  DEST = @op_name(SRC)
2661  """
2662  lbl_src_null = m2_expr.ExprId(ir.gen_label(), instr.mode)
2663  lbl_src_not_null = m2_expr.ExprId(ir.gen_label(), instr.mode)
2664  lbl_next = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
2665 
2666  aff_dst = m2_expr.ExprAff(ir.IRDst, lbl_next)
2667  e = [m2_expr.ExprAff(ir.IRDst, m2_expr.ExprCond(b,
2668  lbl_src_not_null,
2669  lbl_src_null))]
2670  e_src_null = []
2671  e_src_null.append(m2_expr.ExprAff(zf, m2_expr.ExprInt_from(zf, 1)))
2672  # XXX destination is undefined
2673  e_src_null.append(aff_dst)
2674 
2675  e_src_not_null = []
2676  e_src_not_null.append(m2_expr.ExprAff(zf, m2_expr.ExprInt_from(zf, 0)))
2677  e_src_not_null.append(m2_expr.ExprAff(a, m2_expr.ExprOp(op_name, b)))
2678  e_src_not_null.append(aff_dst)
2679 
2680  return e, [irbloc(lbl_src_null.name, [e_src_null]),
2681  irbloc(lbl_src_not_null.name, [e_src_not_null])]
2682 
2683 def bsf(ir, instr, a, b):
2684  return bsr_bsf(ir, instr, a, b, "bsf")
2685 
2686 def bsr(ir, instr, a, b):
2687  return bsr_bsf(ir, instr, a, b, "bsr")
2688 
2689 
2690 def arpl(ir, instr, a, b):
2691  e = []
2692  e.append(m2_expr.ExprAff(exception_flags, m2_expr.ExprInt32(1 << 7)))
2693  return e, []
2694 
2695 
2696 def ins(ir, instr, size):
2697  e = []
2698  e.append(m2_expr.ExprAff(exception_flags, m2_expr.ExprInt32(1 << 7)))
2699  return e, []
2700 
2701 
2702 def sidt(ir, instr, a):
2703  e = []
2704  if not isinstance(a, m2_expr.ExprMem) or a.size != 32:
2705  raise ValueError('not exprmem 32bit instance!!')
2706  b = a.arg
2707  print "DEFAULT SIDT ADDRESS %s!!" % str(a)
2708  e.append(m2_expr.ExprAff(m2_expr.ExprMem(b, 32),
2709  m2_expr.ExprInt32(0xe40007ff)))
2710  e.append(
2711  m2_expr.ExprAff(m2_expr.ExprMem(m2_expr.ExprOp("+", b,
2712  m2_expr.ExprInt_from(b, 4)), 16), m2_expr.ExprInt16(0x8245)))
2713  return e, []
2714 
2715 
2716 def sldt(ir, instr, a):
2717  # XXX TOOD
2718  e = [m2_expr.ExprAff(exception_flags, m2_expr.ExprInt32(EXCEPT_PRIV_INSN))]
2719  return e, []
2720 
2721 
2722 def cmovz(ir, instr, arg1, arg2):
2723  return gen_cmov(ir, instr, zf, arg1, arg2, True)
2724 
2725 def cmovnz(ir, instr, arg1, arg2):
2726  return gen_cmov(ir, instr, zf, arg1, arg2, False)
2727 
2728 
2729 def cmovpe(ir, instr, arg1, arg2):
2730  return gen_cmov(ir, instr, pf, arg1, arg2, True)
2731 
2732 
2733 def cmovnp(ir, instr, arg1, arg2):
2734  return gen_cmov(ir, instr, pf, arg1, arg2, False)
2735 
2736 
2737 def cmovge(ir, instr, arg1, arg2):
2738  return gen_cmov(ir, instr, nf^of, arg1, arg2, False)
2739 
2740 
2741 def cmovg(ir, instr, arg1, arg2):
2742  return gen_cmov(ir, instr, zf|(nf^of), arg1, arg2, False)
2743 
2744 
2745 def cmovl(ir, instr, arg1, arg2):
2746  return gen_cmov(ir, instr, nf^of, arg1, arg2, True)
2747 
2748 
2749 def cmovle(ir, instr, arg1, arg2):
2750  return gen_cmov(ir, instr, zf|(nf^of), arg1, arg2, True)
2751 
2752 
2753 def cmova(ir, instr, arg1, arg2):
2754  return gen_cmov(ir, instr, cf|zf, arg1, arg2, False)
2755 
2756 
2757 def cmovae(ir, instr, arg1, arg2):
2758  return gen_cmov(ir, instr, cf, arg1, arg2, False)
2759 
2760 
2761 def cmovbe(ir, instr, arg1, arg2):
2762  return gen_cmov(ir, instr, cf|zf, arg1, arg2, True)
2763 
2764 
2765 def cmovb(ir, instr, arg1, arg2):
2766  return gen_cmov(ir, instr, cf, arg1, arg2, True)
2767 
2768 
2769 def cmovo(ir, instr, arg1, arg2):
2770  return gen_cmov(ir, instr, of, arg1, arg2, True)
2771 
2772 
2773 def cmovno(ir, instr, arg1, arg2):
2774  return gen_cmov(ir, instr, of, arg1, arg2, False)
2775 
2776 
2777 def cmovs(ir, instr, arg1, arg2):
2778  return gen_cmov(ir, instr, nf, arg1, arg2, True)
2779 
2780 
2781 def cmovns(ir, instr, arg1, arg2):
2782  return gen_cmov(ir, instr, nf, arg1, arg2, False)
2783 
2784 
2785 def icebp(ir, instr):
2786  e = []
2787  e.append(m2_expr.ExprAff(exception_flags,
2788  m2_expr.ExprInt32(EXCEPT_PRIV_INSN)))
2789  return e, []
2790 # XXX
2791 
2792 
2793 def l_int(ir, instr, a):
2794  e = []
2795  # XXX
2796  if a.arg in [1, 3]:
2797  except_int = EXCEPT_SOFT_BP
2798  else:
2799  except_int = EXCEPT_INT_XX
2800  e.append(m2_expr.ExprAff(exception_flags,
2801  m2_expr.ExprInt32(except_int)))
2802  return e, []
2803 
2804 
2805 def l_sysenter(ir, instr):
2806  e = []
2807  e.append(m2_expr.ExprAff(exception_flags,
2808  m2_expr.ExprInt32(EXCEPT_PRIV_INSN)))
2809  return e, []
2810 
2811 # XXX
2812 
2813 
2814 def l_out(ir, instr, a, b):
2815  e = []
2816  e.append(m2_expr.ExprAff(exception_flags,
2817  m2_expr.ExprInt32(EXCEPT_PRIV_INSN)))
2818  return e, []
2819 
2820 # XXX
2821 
2822 
2823 def l_outs(ir, instr, size):
2824  e = []
2825  e.append(m2_expr.ExprAff(exception_flags,
2826  m2_expr.ExprInt32(EXCEPT_PRIV_INSN)))
2827  return e, []
2828 
2829 # XXX actually, xlat performs al = (ds:[e]bx + ZeroExtend(al))
2830 
2831 
2832 def xlat(ir, instr):
2833  e = []
2834  a = m2_expr.ExprCompose([(m2_expr.ExprInt(0, 24), 8, 32),
2835  (mRAX[instr.mode][0:8], 0, 8)])
2836  b = m2_expr.ExprMem(m2_expr.ExprOp('+', mRBX[instr.mode], a), 8)
2837  e.append(m2_expr.ExprAff(mRAX[instr.mode][0:8], b))
2838  return e, []
2839 
2840 
2841 def cpuid(ir, instr):
2842  e = []
2843  e.append(
2844  m2_expr.ExprAff(mRAX[instr.mode],
2845  m2_expr.ExprOp('cpuid', mRAX[instr.mode], m2_expr.ExprInt(0, instr.mode))))
2846  e.append(
2847  m2_expr.ExprAff(mRBX[instr.mode],
2848  m2_expr.ExprOp('cpuid', mRAX[instr.mode], m2_expr.ExprInt(1, instr.mode))))
2849  e.append(
2850  m2_expr.ExprAff(mRCX[instr.mode],
2851  m2_expr.ExprOp('cpuid', mRAX[instr.mode], m2_expr.ExprInt(2, instr.mode))))
2852  e.append(
2853  m2_expr.ExprAff(mRDX[instr.mode],
2854  m2_expr.ExprOp('cpuid', mRAX[instr.mode], m2_expr.ExprInt(3, instr.mode))))
2855  return e, []
2856 
2857 
2858 def bittest_get(a, b):
2859  b = b.zeroExtend(a.size)
2860  if isinstance(a, m2_expr.ExprMem):
2861  b_mask = {16:4, 32:5, 64:6}
2862  b_decal = {16:1, 32:3, 64:7}
2863  ptr = a.arg
2864  off_bit = b.zeroExtend(a.size) & m2_expr.ExprInt((1<<b_mask[a.size])-1,
2865  a.size)
2866  off_byte = ((b.zeroExtend(ptr.size) >> m2_expr.ExprInt_from(ptr, 3)) &
2867  m2_expr.ExprInt_from(ptr,
2868  ((1<<a.size)-1) ^ b_decal[a.size]))
2869 
2870  d = m2_expr.ExprMem(ptr + off_byte, a.size)
2871  else:
2872  off_bit = m2_expr.ExprOp('&', b, m2_expr.ExprInt_from(a, a.size - 1))
2873  d = a
2874  return d, off_bit
2875 
2876 
2877 def bt(ir, instr, a, b):
2878  e = []
2879  b = b.zeroExtend(a.size)
2880  d, off_bit = bittest_get(a, b)
2881  d = d >> off_bit
2882  e.append(m2_expr.ExprAff(cf, d[:1]))
2883  return e, []
2884 
2885 
2886 def btc(ir, instr, a, b):
2887  e = []
2888  d, off_bit = bittest_get(a, b)
2889  e.append(m2_expr.ExprAff(cf, (d >> off_bit)[:1]))
2890 
2891  m = m2_expr.ExprInt_from(a, 1) << off_bit
2892  e.append(m2_expr.ExprAff(d, d ^ m))
2893 
2894  return e, []
2895 
2896 
2897 def bts(ir, instr, a, b):
2898  e = []
2899  d, off_bit = bittest_get(a, b)
2900  e.append(m2_expr.ExprAff(cf, (d >> off_bit)[:1]))
2901  m = m2_expr.ExprInt_from(a, 1) << off_bit
2902  e.append(m2_expr.ExprAff(d, d | m))
2903 
2904  return e, []
2905 
2906 
2907 def btr(ir, instr, a, b):
2908  e = []
2909  d, off_bit = bittest_get(a, b)
2910  e.append(m2_expr.ExprAff(cf, (d >> off_bit)[:1]))
2911  m = ~(m2_expr.ExprInt_from(a, 1) << off_bit)
2912  e.append(m2_expr.ExprAff(d, d & m))
2913 
2914  return e, []
2915 
2916 
2917 def into(ir, instr):
2918  return [], None
2919 
2920 
2921 def l_in(ir, instr, a, b):
2922  e = []
2923  e.append(m2_expr.ExprAff(exception_flags,
2924  m2_expr.ExprInt32(EXCEPT_PRIV_INSN)))
2925  return e, []
2926 
2927 
2928 def cmpxchg(ir, instr, a, b):
2929  e = []
2930 
2931  c = mRAX[instr.mode][:a.size]
2932  cond = c - a
2933  e.append(
2934  m2_expr.ExprAff(zf,
2935  m2_expr.ExprCond(cond,
2936  m2_expr.ExprInt_from(zf, 0),
2937  m2_expr.ExprInt_from(zf, 1))))
2938  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond(cond,
2939  b,
2940  a)
2941  ))
2942  e.append(m2_expr.ExprAff(c, m2_expr.ExprCond(cond,
2943  a,
2944  c)
2945  ))
2946  return e, []
2947 
2948 
2949 def lds(ir, instr, a, b):
2950  e = []
2951  e.append(m2_expr.ExprAff(a, m2_expr.ExprMem(b.arg, size=a.size)))
2952  DS_value = m2_expr.ExprMem(b.arg + m2_expr.ExprInt_from(b.arg, a.size/8),
2953  size=16)
2954  e.append(m2_expr.ExprAff(DS, DS_value))
2955  return e, []
2956 
2957 
2958 def les(ir, instr, a, b):
2959  e = []
2960  e.append(m2_expr.ExprAff(a, m2_expr.ExprMem(b.arg, size=a.size)))
2961  ES_value = m2_expr.ExprMem(b.arg + m2_expr.ExprInt_from(b.arg, a.size/8),
2962  size=16)
2963  e.append(m2_expr.ExprAff(ES, ES_value))
2964  return e, []
2965 
2966 
2967 def lss(ir, instr, a, b):
2968  e = []
2969  e.append(m2_expr.ExprAff(a, m2_expr.ExprMem(b.arg, size=a.size)))
2970  SS_value = m2_expr.ExprMem(b.arg + m2_expr.ExprInt_from(b.arg, a.size/8),
2971  size=16)
2972  e.append(m2_expr.ExprAff(SS, SS_value))
2973  return e, []
2974 
2975 def lfs(ir, instr, a, b):
2976  e = []
2977  e.append(m2_expr.ExprAff(a, m2_expr.ExprMem(b.arg, size=a.size)))
2978  FS_value = m2_expr.ExprMem(b.arg + m2_expr.ExprInt_from(b.arg, a.size/8),
2979  size=16)
2980  e.append(m2_expr.ExprAff(FS, FS_value))
2981  return e, []
2982 
2983 def lgs(ir, instr, a, b):
2984  e = []
2985  e.append(m2_expr.ExprAff(a, m2_expr.ExprMem(b.arg, size=a.size)))
2986  GS_value = m2_expr.ExprMem(b.arg + m2_expr.ExprInt_from(b.arg, a.size/8),
2987  size=16)
2988  e.append(m2_expr.ExprAff(GS, GS_value))
2989  return e, []
2990 
2991 
2992 def lahf(ir, instr):
2993  e = []
2994  args = []
2995  regs = [cf, m2_expr.ExprInt1(1), pf, m2_expr.ExprInt1(0), af,
2996  m2_expr.ExprInt1(0), zf, nf]
2997  for i in xrange(len(regs)):
2998  args.append((regs[i], i, i + 1))
2999  e.append(m2_expr.ExprAff(mRAX[instr.mode][8:16], m2_expr.ExprCompose(args)))
3000  return e, []
3001 
3002 
3003 def sahf(ir, instr):
3004  tmp = mRAX[instr.mode][8:16]
3005  e = []
3006  e.append(m2_expr.ExprAff(cf, tmp[0:1]))
3007  e.append(m2_expr.ExprAff(pf, tmp[2:3]))
3008  e.append(m2_expr.ExprAff(af, tmp[4:5]))
3009  e.append(m2_expr.ExprAff(zf, tmp[6:7]))
3010  e.append(m2_expr.ExprAff(nf, tmp[7:8]))
3011  return e, []
3012 
3013 
3014 def lar(ir, instr, a, b):
3015  e = []
3016  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('access_segment', b)))
3017  e.append(m2_expr.ExprAff(zf, m2_expr.ExprOp('access_segment_ok', b)))
3018  return e, []
3019 
3020 
3021 def lsl(ir, instr, a, b):
3022  e = []
3023  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('load_segment_limit', b)))
3024  e.append(m2_expr.ExprAff(zf, m2_expr.ExprOp('load_segment_limit_ok', b)))
3025  return e, []
3026 
3027 
3028 def fclex(ir, instr):
3029  # XXX TODO
3030  return [], None
3031 
3032 
3033 def fnclex(ir, instr):
3034  # XXX TODO
3035  return [], None
3036 
3037 
3038 def l_str(ir, instr, a):
3039  e = []
3040  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('load_tr_segment_selector',
3041  m2_expr.ExprInt32(0))))
3042  return e, []
3043 
3044 
3045 def movd(ir, instr, a, b):
3046  e = []
3047  if a in regs_mm_expr:
3048  e.append(m2_expr.ExprAff(a, m2_expr.ExprCompose([(b, 0, 32),
3049  (m2_expr.ExprInt32(0), 32, 64)])))
3050  elif a in regs_xmm_expr:
3051  e.append(m2_expr.ExprAff(a, m2_expr.ExprCompose([(b, 0, 32),
3052  (m2_expr.ExprInt(0, 96), 32, 128)])))
3053  else:
3054  e.append(m2_expr.ExprAff(a, b[:32]))
3055  return e, []
3056 
3057 def movdqu(ir, instr, a, b):
3058  # XXX TODO alignement check
3059  return [m2_expr.ExprAff(a, b)], []
3060 
3061 
3062 def movapd(ir, instr, a, b):
3063  # XXX TODO alignement check
3064  return [m2_expr.ExprAff(a, b)], []
3065 
3066 
3067 def andps(ir, instr, a, b):
3068  e = []
3069  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('&', a, b)))
3070  return e, []
3071 
3072 
3073 def orps(ir, instr, a, b):
3074  e = []
3075  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('|', a, b)))
3076  return e, []
3077 
3078 
3079 def xorps(ir, instr, a, b):
3080  e = []
3081  e.append(m2_expr.ExprAff(a, m2_expr.ExprOp('^', a, b)))
3082  return e, []
3083 
3084 
3085 def rdmsr(ir, instr):
3086  msr_addr = m2_expr.ExprId('MSR') + m2_expr.ExprInt32(8) * mRCX[instr.mode][:32]
3087  e = []
3088  e.append(m2_expr.ExprAff(mRAX[instr.mode][:32], m2_expr.ExprMem(msr_addr, 32)))
3089  e.append(m2_expr.ExprAff(mRDX[instr.mode][:32], m2_expr.ExprMem(msr_addr + m2_expr.ExprInt_from(msr_addr, 4), 32)))
3090  return e, []
3091 
3092 def wrmsr(ir, instr):
3093  msr_addr = m2_expr.ExprId('MSR') + m2_expr.ExprInt32(8) * mRCX[instr.mode][:32]
3094  e = []
3095  src = m2_expr.ExprCompose([(mRAX[instr.mode][:32], 0, 32),
3096  (mRDX[instr.mode][:32], 32, 64)])
3097  e.append(m2_expr.ExprAff(m2_expr.ExprMem(msr_addr, 64), src))
3098  return e, []
3099 
3100 ### MMX/SSE/AVX operations
3101 ###
3102 
3103 def vec_op_clip(op, size):
3104  """
3105  Generate simd operations
3106  @op: the operator
3107  @size: size of an element
3108  """
3109  def vec_op_clip_instr(ir, instr, a, b):
3110  if op == '-':
3111  return [m2_expr.ExprAff(a[:size], a[:size] - b[:size])], []
3112  else:
3113  return [m2_expr.ExprAff(a[:size], m2_expr.ExprOp(op, a[:size], b[:size]))], []
3114  return vec_op_clip_instr
3115 
3116 # Generic vertical operation
3117 def vec_vertical_sem(op, elt_size, reg_size, a, b):
3118  assert(reg_size % elt_size == 0)
3119  n = reg_size/elt_size
3120  if op == '-':
3121  ops = [((a[i*elt_size:(i+1)*elt_size] - b[i*elt_size:(i+1)*elt_size]),
3122  i*elt_size, (i+1)*elt_size) for i in xrange(0, n)]
3123  else:
3124  ops = [(m2_expr.ExprOp(op, a[i*elt_size:(i+1)*elt_size],
3125  b[i*elt_size:(i+1)*elt_size]),
3126  i*elt_size,
3127  (i+1)*elt_size) for i in xrange(0, n)]
3128 
3129  return m2_expr.ExprCompose(ops)
3130 
3131 def float_vec_vertical_sem(op, elt_size, reg_size, a, b):
3132  assert(reg_size % elt_size == 0)
3133  n = reg_size/elt_size
3134 
3135  x_to_int, int_to_x = {32: ('float_to_int_%d', 'int_%d_to_float'),
3136  64: ('double_to_int_%d', 'int_%d_to_double')}[elt_size]
3137  if op == '-':
3138  ops = [(m2_expr.ExprOp(x_to_int % elt_size,
3139  m2_expr.ExprOp(int_to_x % elt_size, a[i*elt_size:(i+1)*elt_size]) -
3140  m2_expr.ExprOp(int_to_x % elt_size, b[i*elt_size:(i+1)*elt_size])),
3141  i*elt_size, (i+1)*elt_size) for i in xrange(0, n)]
3142  else:
3143  ops = [(m2_expr.ExprOp(x_to_int % elt_size,
3144  m2_expr.ExprOp(op,
3145  m2_expr.ExprOp(int_to_x % elt_size, a[i*elt_size:(i+1)*elt_size]),
3146  m2_expr.ExprOp(int_to_x % elt_size, b[i*elt_size:(i+1)*elt_size]))),
3147  i*elt_size, (i+1)*elt_size) for i in xrange(0, n)]
3148 
3149  return m2_expr.ExprCompose(ops)
3150 
3151 def __vec_vertical_instr_gen(op, elt_size, sem):
3152  def vec_instr(ir, instr, a, b):
3153  e = []
3154  if isinstance(b, m2_expr.ExprMem):
3155  b = m2_expr.ExprMem(b.arg, a.size)
3156  reg_size = a.size
3157  e.append(m2_expr.ExprAff(a, sem(op, elt_size, reg_size, a, b)))
3158  return e, []
3159  return vec_instr
3160 
3161 def vec_vertical_instr(op, elt_size):
3162  return __vec_vertical_instr_gen(op, elt_size, vec_vertical_sem)
3163 
3164 def float_vec_vertical_instr(op, elt_size):
3165  return __vec_vertical_instr_gen(op, elt_size, float_vec_vertical_sem)
3166 
3167 ### Integer arithmetic
3168 ###
3169 
3170 ## Additions
3171 ##
3172 
3173 # SSE
3174 paddb = vec_vertical_instr('+', 8)
3175 paddw = vec_vertical_instr('+', 16)
3176 paddd = vec_vertical_instr('+', 32)
3177 paddq = vec_vertical_instr('+', 64)
3178 
3179 ## Substractions
3180 ##
3181 
3182 # SSE
3183 psubb = vec_vertical_instr('-', 8)
3184 psubw = vec_vertical_instr('-', 16)
3185 psubd = vec_vertical_instr('-', 32)
3186 psubq = vec_vertical_instr('-', 64)
3187 
3188 ### Floating-point arithmetic
3189 ###
3190 
3191 # SSE
3192 addss = vec_op_clip('+', 32)
3193 addsd = vec_op_clip('+', 64)
3196 subss = vec_op_clip('-', 32)
3197 subsd = vec_op_clip('-', 64)
3200 mulss = vec_op_clip('*', 32)
3201 mulsd = vec_op_clip('*', 64)
3204 divss = vec_op_clip('/', 32)
3205 divsd = vec_op_clip('/', 64)
3208 
3209 ### Logical (floating-point)
3210 ###
3211 
3212 # MMX/SSE/AVX
3213 def pand(ir, instr, a, b):
3214  e = []
3215  c = a & b
3216  # No flag affected
3217  e.append(m2_expr.ExprAff(a, c))
3218  return e, []
3219 
3220 
3221 def por(ir, instr, a, b):
3222  e = []
3223  c = a | b
3224  e.append(m2_expr.ExprAff(a, c))
3225  return e, []
3226 
3227 
3228 def pminsw(ir, instr, a, b):
3229  e = []
3230  e.append(m2_expr.ExprAff(a, m2_expr.ExprCond((a - b).msb(), a, b)))
3231  return e, []
3232 
3233 def cvtdq2pd(ir, instr, a, b):
3234  e = []
3235  e.append(m2_expr.ExprAff(a[:64], m2_expr.ExprOp('int_32_to_double', b[:32])))
3236  e.append(m2_expr.ExprAff(a[64:128], m2_expr.ExprOp('int_32_to_double', b[32:64])))
3237  return e, []
3238 
3239 def cvtdq2ps(ir, instr, a, b):
3240  e = []
3241  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('int_32_to_float', b[:32])))
3242  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('int_32_to_float', b[32:64])))
3243  e.append(m2_expr.ExprAff(a[64:96], m2_expr.ExprOp('int_32_to_float', b[64:96])))
3244  e.append(m2_expr.ExprAff(a[96:128], m2_expr.ExprOp('int_32_to_float', b[96:128])))
3245  return e, []
3246 
3247 def cvtpd2dq(ir, instr, a, b):
3248  e = []
3249  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_to_int_32', b[:64])))
3250  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('double_to_int_32', b[64:128])))
3251  e.append(m2_expr.ExprAff(a[64:128], m2_expr.ExprInt64(0)))
3252  return e, []
3253 
3254 def cvtpd2pi(ir, instr, a, b):
3255  e = []
3256  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_to_int_32', b[:64])))
3257  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('double_to_int_32', b[64:128])))
3258  return e, []
3259 
3260 def cvtpd2ps(ir, instr, a, b):
3261  e = []
3262  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_to_float', b[:64])))
3263  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('double_to_float', b[64:128])))
3264  e.append(m2_expr.ExprAff(a[64:128], m2_expr.ExprInt64(0)))
3265  return e, []
3266 
3267 def cvtpi2pd(ir, instr, a, b):
3268  e = []
3269  e.append(m2_expr.ExprAff(a[:64], m2_expr.ExprOp('int_32_to_double', b[:32])))
3270  e.append(m2_expr.ExprAff(a[64:128], m2_expr.ExprOp('int_32_to_double', b[32:64])))
3271  return e, []
3272 
3273 def cvtpi2ps(ir, instr, a, b):
3274  e = []
3275  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('int_32_to_float', b[:32])))
3276  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('int_32_to_float', b[32:64])))
3277  return e, []
3278 
3279 def cvtps2dq(ir, instr, a, b):
3280  e = []
3281  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('float_to_int_32', b[:32])))
3282  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('float_to_int_32', b[32:64])))
3283  e.append(m2_expr.ExprAff(a[64:96], m2_expr.ExprOp('float_to_int_32', b[64:96])))
3284  e.append(m2_expr.ExprAff(a[96:128], m2_expr.ExprOp('float_to_int_32', b[96:128])))
3285  return e, []
3286 
3287 def cvtps2pd(ir, instr, a, b):
3288  e = []
3289  e.append(m2_expr.ExprAff(a[:64], m2_expr.ExprOp('float_to_double', b[:32])))
3290  e.append(m2_expr.ExprAff(a[64:128], m2_expr.ExprOp('float_to_double', b[32:64])))
3291  return e, []
3292 
3293 def cvtps2pi(ir, instr, a, b):
3294  e = []
3295  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('float_to_int_32', b[:32])))
3296  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('float_to_int_32', b[32:64])))
3297  return e, []
3298 
3299 def cvtsd2si(ir, instr, a, b):
3300  e = []
3301  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_to_int_32', b[:64])))
3302  return e, []
3303 
3304 def cvtsd2ss(ir, instr, a, b):
3305  e = []
3306  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_to_float', b[:64])))
3307  return e, []
3308 
3309 def cvtsi2sd(ir, instr, a, b):
3310  e = []
3311  e.append(m2_expr.ExprAff(a[:64], m2_expr.ExprOp('int_32_to_double', b[:32])))
3312  return e, []
3313 
3314 def cvtsi2ss(ir, instr, a, b):
3315  e = []
3316  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('int_32_to_float', b[:32])))
3317  return e, []
3318 
3319 def cvtss2sd(ir, instr, a, b):
3320  e = []
3321  e.append(m2_expr.ExprAff(a[:64], m2_expr.ExprOp('float_to_double', b[:32])))
3322  return e, []
3323 
3324 def cvtss2si(ir, instr, a, b):
3325  e = []
3326  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('float_to_int_32', b[:32])))
3327  return e, []
3328 
3329 def cvttpd2pi(ir, instr, a, b):
3330  e = []
3331  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_trunc_to_int_32', b[:64])))
3332  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('double_trunc_to_int_32', b[64:128])))
3333  return e, []
3334 
3335 def cvttpd2dq(ir, instr, a, b):
3336  e = []
3337  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_trunc_to_int_32', b[:64])))
3338  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('double_trunc_to_int_32', b[64:128])))
3339  e.append(m2_expr.ExprAff(a[64:128], m2_expr.ExprInt64(0)))
3340  return e, []
3341 
3342 def cvttps2dq(ir, instr, a, b):
3343  e = []
3344  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('float_trunc_to_int_32', b[:32])))
3345  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('float_trunc_to_int_32', b[32:64])))
3346  e.append(m2_expr.ExprAff(a[64:96], m2_expr.ExprOp('float_trunc_to_int_32', b[64:96])))
3347  e.append(m2_expr.ExprAff(a[96:128], m2_expr.ExprOp('float_trunc_to_int_32', b[96:128])))
3348  return e, []
3349 
3350 def cvttps2pi(ir, instr, a, b):
3351  e = []
3352  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('float_trunc_to_int_32', b[:32])))
3353  e.append(m2_expr.ExprAff(a[32:64], m2_expr.ExprOp('float_trunc_to_int_32', b[32:64])))
3354  return e, []
3355 
3356 def cvttsd2si(ir, instr, a, b):
3357  e = []
3358  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('double_trunc_to_int_32', b[:64])))
3359  return e, []
3360 
3361 def cvttss2si(ir, instr, a, b):
3362  e = []
3363  e.append(m2_expr.ExprAff(a[:32], m2_expr.ExprOp('float_trunc_to_int_32', b[:32])))
3364  return e, []
3365 
3366 def movss(ir, instr, a, b):
3367  e = []
3368  if not isinstance(a, m2_expr.ExprMem) and not isinstance(b, m2_expr.ExprMem):
3369  # Source and Destination xmm
3370  e.append(m2_expr.ExprAff(a[:32], b[:32]))
3371  elif not isinstance(b, m2_expr.ExprMem) and isinstance(a, m2_expr.ExprMem):
3372  # Source XMM Destination Mem
3373  e.append(m2_expr.ExprAff(a, b[:32]))
3374  else:
3375  # Source Mem Destination XMM
3376  e.append(m2_expr.ExprAff(a, m2_expr.ExprCompose([(b, 0, 32),
3377  (m2_expr.ExprInt(0, 96), 32, 128)])))
3378  return e, []
3379 
3380 
3381 def ucomiss(ir, instr, a, b):
3382  e = []
3383  e.append(m2_expr.ExprAff(zf, m2_expr.ExprOp('ucomiss_zf', a[:32], b[:32])))
3384  e.append(m2_expr.ExprAff(pf, m2_expr.ExprOp('ucomiss_pf', a[:32], b[:32])))
3385  e.append(m2_expr.ExprAff(cf, m2_expr.ExprOp('ucomiss_cf', a[:32], b[:32])))
3386 
3387  e.append(m2_expr.ExprAff(of, m2_expr.ExprInt1(0)))
3388  e.append(m2_expr.ExprAff(af, m2_expr.ExprInt1(0)))
3389  e.append(m2_expr.ExprAff(nf, m2_expr.ExprInt1(0)))
3390 
3391  return e, []
3392 
3393 mnemo_func = {'mov': mov,
3394  'xchg': xchg,
3395  'movzx': movzx,
3396  'movsx': movsx,
3397  'movsxd': movsx,
3398  'lea': lea,
3399  'add': add,
3400  'xadd': xadd,
3401  'adc': adc,
3402  'sub': sub,
3403  'sbb': sbb,
3404  'neg': neg,
3405  'not': l_not,
3406  'cmp': l_cmp,
3407  'xor': xor,
3408  'pxor': pxor,
3409  'or': l_or,
3410  'and': l_and,
3411  'test': l_test,
3412  'rol': l_rol,
3413  'ror': l_ror,
3414  'rcl': rcl,
3415  'rcr': rcr,
3416  'sar': sar,
3417  'shr': shr,
3418  'shrd_cl': shrd_cl,
3419  'sal': sal,
3420  'shl': shl,
3421  'shld_cl': shld_cl,
3422  'shld': shld,
3423  'cmc': cmc,
3424  'clc': clc,
3425  'stc': stc,
3426  'cld': cld,
3427  'std': std,
3428  'cli': cli,
3429  'sti': sti,
3430  'bsf': bsf,
3431  'bsr': bsr,
3432  'inc': inc,
3433  'dec': dec,
3434  'push': push,
3435  'pushw': pushw,
3436  'pop': pop,
3437  'popw': popw,
3438  'sete': sete,
3439  'setnz': setnz,
3440  'setl': setl,
3441  'setg': setg,
3442  'setge': setge,
3443  'seta': seta,
3444  'setae': setae,
3445  'setb': setb,
3446  'setbe': setbe,
3447  'setns': setns,
3448  'sets': sets,
3449  'seto': seto,
3450  'setp': setp,
3451  'setpe': setp,
3452  'setnp': setnp,
3453  'setpo': setnp,
3454  'setle': setle,
3455  'setng': setle,
3456  'setna': setna,
3457  'setnbe': setnbe,
3458  'setno': setno,
3459  'setnc': setnb,
3460  'setz': sete,
3461  'setne': setnz,
3462  'setnb': setae,
3463  'setnae': setb,
3464  'setc': setb,
3465  'setnge': setl,
3466  'setnl': setge,
3467  'setnle': setg,
3468  'setalc': setalc,
3469  'bswap': bswap,
3470  'cmpsb': lambda ir, instr: cmps(ir, instr, 8),
3471  'cmpsw': lambda ir, instr: cmps(ir, instr, 16),
3472  'cmpsd': lambda ir, instr: cmps(ir, instr, 32),
3473  'scasb': lambda ir, instr: scas(ir, instr, 8),
3474  'scasw': lambda ir, instr: scas(ir, instr, 16),
3475  'scasd': lambda ir, instr: scas(ir, instr, 32),
3476  'pushfd': pushfd,
3477  'pushfq': pushfq,
3478  'pushfw': pushfw,
3479  'popfd': popfd,
3480  'popfq': popfd,
3481  'popfw': popfw,
3482  'pushad': pushad,
3483  'pusha': pushad,
3484  'popad': popad,
3485  'popa': popad,
3486  'call': call,
3487  'ret': ret,
3488  'retf': retf,
3489  'leave': leave,
3490  'enter': enter,
3491  'jmp': jmp,
3492  'jmpf': jmpf,
3493  'jz': jz,
3494  'je': jz,
3495  'jcxz': jcxz,
3496  'jecxz': jecxz,
3497  'jrcxz': jrcxz,
3498  'jnz': jnz,
3499  'jp': jp,
3500  'jpe': jp,
3501  'jnp': jnp,
3502  'ja': ja,
3503  'jae': jae,
3504  'jb': jb,
3505  'jbe': jbe,
3506  'jg': jg,
3507  'jge': jge,
3508  'jl': jl,
3509  'jle': jle,
3510  'js': js,
3511  'jns': jns,
3512  'jo': jo,
3513  'jno': jno,
3514  'jecxz': jecxz,
3515  'loop': loop,
3516  'loopne': loopne,
3517  'loope': loope,
3518  'div': div,
3519  'mul': mul,
3520  'imul': imul,
3521  'idiv': idiv,
3522 
3523  'cbw': cbw,
3524  'cwde': cwde,
3525  'cdqe': cdqe,
3526 
3527  'cwd': cwd,
3528  'cdq': cdq,
3529  'cqo': cqo,
3530 
3531  'daa': daa,
3532  'das': das,
3533  'aam': aam,
3534  'aad': aad,
3535  'aaa': aaa,
3536  'aas': aas,
3537  'shrd': shrd,
3538  'stosb': lambda ir, instr: stos(ir, instr, 8),
3539  'stosw': lambda ir, instr: stos(ir, instr, 16),
3540  'stosd': lambda ir, instr: stos(ir, instr, 32),
3541  'stosq': lambda ir, instr: stos(ir, instr, 64),
3542 
3543  'lodsb': lambda ir, instr: lods(ir, instr, 8),
3544  'lodsw': lambda ir, instr: lods(ir, instr, 16),
3545  'lodsd': lambda ir, instr: lods(ir, instr, 32),
3546  'lodsq': lambda ir, instr: lods(ir, instr, 64),
3547 
3548  'movsb': lambda ir, instr: movs(ir, instr, 8),
3549  'movsw': lambda ir, instr: movs(ir, instr, 16),
3550  'movsd': movsd_dispatch,
3551  'movsq': lambda ir, instr: movs(ir, instr, 64),
3552  'fcomp': fcomp,
3553  'fcompp': fcompp,
3554  'ficomp': ficomp,
3555  'fucom': fucom,
3556  'fucomp': fucomp,
3557  'fucompp': fucompp,
3558  'comiss': comiss,
3559  'comisd': comisd,
3560  'fcomi': fcomi,
3561  'fcomip': fcomip,
3562  'nop': nop,
3563  'fnop': nop, # XXX
3564  'hlt': hlt,
3565  'rdtsc': rdtsc,
3566  'fst': fst,
3567  'fstp': fstp,
3568  'fist': fist,
3569  'fistp': fistp,
3570  'fisttp': fisttp,
3571  'fld': fld,
3572  'fldz': fldz,
3573  'fld1': fld1,
3574  'fldl2t': fldl2t,
3575  'fldpi': fldpi,
3576  'fldln2': fldln2,
3577  'fldl2e': fldl2e,
3578  'fldlg2': fldlg2,
3579  'fild': fild,
3580  'fadd': fadd,
3581  'fiadd': fiadd,
3582  'fisub': fisub,
3583  'fisubr': fisubr,
3584  'fpatan': fpatan,
3585  'fprem': fprem,
3586  'fprem1': fprem1,
3587  'fninit': fninit,
3588  'fyl2x': fyl2x,
3589  'faddp': faddp,
3590  'fsub': fsub,
3591  'fsubp': fsubp,
3592  'fsubr': fsubr,
3593  'fsubrp': fsubrp,
3594  'fmul': fmul,
3595  'fimul': fimul,
3596  'fmulp': fmulp,
3597  'fdiv': fdiv,
3598  'fdivr': fdivr,
3599  'fdivrp': fdivrp,
3600  'fidiv': fidiv,
3601  'fidivr': fidivr,
3602  'fdivp': fdivp,
3603  'fxch': fxch,
3604  'fptan': fptan,
3605  'frndint': frndint,
3606  'fsin': fsin,
3607  'fcos': fcos,
3608  'fsincos': fsincos,
3609  'fscale': fscale,
3610  'f2xm1': f2xm1,
3611  'fchs': fchs,
3612  'fsqrt': fsqrt,
3613  'fabs': fabs,
3614  'fnstsw': fnstsw,
3615  'fnstcw': fnstcw,
3616  'fldcw': fldcw,
3617  'fwait': fwait,
3618  'fcmovb': fcmovb,
3619  'fcmove': fcmove,
3620  'fcmovbe': fcmovbe,
3621  'fcmovu': fcmovu,
3622  'fcmovnb': fcmovnb,
3623  'fcmovne': fcmovne,
3624  'fcmovnbe': fcmovnbe,
3625  'fcmovnu': fcmovnu,
3626  'fnstenv': fnstenv,
3627  'sidt': sidt,
3628  'sldt': sldt,
3629  'arpl': arpl,
3630  'cmovz': cmovz,
3631  'cmove': cmovz,
3632  'cmovnz': cmovnz,
3633  'cmovpe':cmovpe,
3634  'cmovnp':cmovnp,
3635  'cmovge': cmovge,
3636  'cmovnl': cmovge,
3637  'cmovg': cmovg,
3638  'cmovl': cmovl,
3639  'cmova': cmova,
3640  'cmovae': cmovae,
3641  'cmovbe': cmovbe,
3642  'cmovb': cmovb,
3643  'cmovnge': cmovl,
3644  'cmovle': cmovle,
3645  'cmovng': cmovle,
3646  'cmovo': cmovo,
3647  'cmovno': cmovno,
3648  'cmovs': cmovs,
3649  'cmovns': cmovns,
3650  'icebp': icebp,
3651  'int': l_int,
3652  'xlat': xlat,
3653  'bt': bt,
3654  'cpuid': cpuid,
3655  'jo': jo,
3656  'fcom': fcom,
3657  'ftst': ftst,
3658  'fxam': fxam,
3659  'ficom': ficom,
3660  'fcomi': fcomi,
3661  'fcomip': fcomip,
3662  'fucomi': fucomi,
3663  'fucomip': fucomip,
3664  'insb': lambda ir, instr: ins(ir, instr, 8),
3665  'insw': lambda ir, instr: ins(ir, instr, 16),
3666  'insd': lambda ir, instr: ins(ir, instr, 32),
3667  'btc': btc,
3668  'bts': bts,
3669  'btr': btr,
3670  'into': into,
3671  'in': l_in,
3672  'outsb': lambda ir, instr: l_outs(ir, instr, 8),
3673  'outsw': lambda ir, instr: l_outs(ir, instr, 16),
3674  'outsd': lambda ir, instr: l_outs(ir, instr, 32),
3675 
3676  'out': l_out,
3677  "sysenter": l_sysenter,
3678  "cmpxchg": cmpxchg,
3679  "lds": lds,
3680  "les": les,
3681  "lss": lss,
3682  "lfs": lfs,
3683  "lgs": lgs,
3684  "lahf": lahf,
3685  "sahf": sahf,
3686  "lar": lar,
3687  "lsl": lsl,
3688  "fclex": fclex,
3689  "fnclex": fnclex,
3690  "str": l_str,
3691  "movd": movd,
3692  "movdqu":movdqu,
3693  "movdqa":movdqu,
3694  "movapd": movapd, # XXX TODO alignement check
3695  "movupd": movapd, # XXX TODO alignement check
3696  "movaps": movapd, # XXX TODO alignement check
3697  "movups": movapd, # XXX TODO alignement check
3698  "andps": andps,
3699  "andpd": andps,
3700  "orps": orps,
3701  "orpd": orps,
3702  "xorps": xorps,
3703  "xorpd": xorps,
3704 
3705  "pminsw": pminsw,
3706  "cvtdq2pd": cvtdq2pd,
3707  "cvtdq2ps": cvtdq2ps,
3708  "cvtpd2dq": cvtpd2dq,
3709  "cvtpd2pi": cvtpd2pi,
3710  "cvtpd2ps": cvtpd2ps,
3711  "cvtpi2pd": cvtpi2pd,
3712  "cvtpi2ps": cvtpi2ps,
3713  "cvtps2dq": cvtps2dq,
3714  "cvtps2pd": cvtps2pd,
3715  "cvtps2pi": cvtps2pi,
3716  "cvtsd2si": cvtsd2si,
3717  "cvtsd2ss": cvtsd2ss,
3718  "cvtsi2sd": cvtsi2sd,
3719  "cvtsi2ss": cvtsi2ss,
3720  "cvtss2sd": cvtss2sd,
3721  "cvtss2si": cvtss2si,
3722  "cvttpd2pi": cvttpd2pi,
3723  "cvttpd2dq": cvttpd2dq,
3724  "cvttps2dq": cvttps2dq,
3725  "cvttps2pi": cvttps2pi,
3726  "cvttsd2si": cvttsd2si,
3727  "cvttss2si": cvttss2si,
3728 
3729 
3730 
3731 
3732 
3733 
3734 
3735 
3736 
3737  "movss": movss,
3738 
3739  "ucomiss": ucomiss,
3740 
3741  ####
3742  #### MMX/AVX/SSE operations
3743 
3744  ### Arithmetic (integers)
3745  ###
3746 
3747  ## Additions
3748  # SSE
3749  "paddb": paddb,
3750  "paddw": paddw,
3751  "paddd": paddd,
3752  "paddq": paddq,
3753 
3754  # Substractions
3755  # SSE
3756  "psubb": psubb,
3757  "psubw": psubw,
3758  "psubd": psubd,
3759  "psubq": psubq,
3760 
3761  ### Arithmetic (floating-point)
3762  ###
3763 
3764  ## Additions
3765  # SSE
3766  "addss": addss,
3767  "addsd": addsd,
3768  "addps": addps,
3769  "addpd": addpd,
3770 
3771  ## Substractions
3772  # SSE
3773  "subss": subss,
3774  "subsd": subsd,
3775  "subps": subps,
3776  "subpd": subpd,
3777 
3778  ## Multiplications
3779  # SSE
3780  "mulss": mulss,
3781  "mulsd": mulsd,
3782  "mulps": mulps,
3783  "mulpd": mulpd,
3784 
3785  ## Divisions
3786  # SSE
3787  "divss": divss,
3788  "divsd": divsd,
3789  "divps": divps,
3790  "divpd": divpd,
3791 
3792  ### Logical (floating-point)
3793  ###
3794 
3795  "pand": pand,
3796  "por": por,
3797 
3798  "rdmsr": rdmsr,
3799  "wrmsr": wrmsr,
3800 
3801  }
3802 
3803 
3804 class ir_x86_16(ir):
3805 
3806  def __init__(self, symbol_pool=None):
3807  ir.__init__(self, mn_x86, 16, symbol_pool)
3808  self.do_stk_segm = False
3809  self.do_ds_segm = False
3810  self.do_str_segm = False
3811  self.do_all_segm = False
3812  self.pc = IP
3813  self.sp = SP
3814  self.IRDst = m2_expr.ExprId('IRDst', 16)
3815 
3816  def mod_pc(self, instr, instr_ir, extra_ir):
3817  pass
3818 
3819  def get_ir(self, instr):
3820  args = instr.args[:]
3821  args = [arg.replace_expr(float_replace) for arg in args]
3822  my_ss = None
3823  if self.do_ds_segm:
3824  my_ss = DS
3825  if self.do_all_segm and instr.additional_info.g2.value:
3826  my_ss = {1: CS, 2: SS, 3: DS, 4: ES, 5: FS, 6: GS}[
3827  instr.additional_info.g2.value]
3828  if my_ss is not None:
3829  for i, a in enumerate(args):
3830  if isinstance(a, m2_expr.ExprMem) and not a.is_op_segm():
3831  args[i] = m2_expr.ExprMem(m2_expr.ExprOp('segm', my_ss,
3832  a.arg), a.size)
3833 
3834  if not instr.name.lower() in mnemo_func:
3835  raise NotImplementedError("Mnemonic %s not implemented" % instr.name)
3836 
3837  instr_ir, extra_ir = mnemo_func[
3838  instr.name.lower()](self, instr, *args)
3839  self.mod_pc(instr, instr_ir, extra_ir)
3840 
3841  self.mod_pc(instr, instr_ir, extra_ir)
3842  instr.additional_info.except_on_instr = False
3843  if instr.additional_info.g1.value & 6 == 0 or \
3844  not instr.name in repeat_mn:
3845  return instr_ir, extra_ir
3846  if instr.name == "MOVSD" and len(instr.args) == 2:
3847  return instr_ir, extra_ir
3848 
3849  instr.additional_info.except_on_instr = True
3850  # get instruction size
3851  s = {"B": 8, "W": 16, "D": 32, 'Q': 64}[instr.name[-1]]
3852  size = instr.v_opmode()
3853  c_reg = mRCX[instr.mode][:size]
3854  out_ir = []
3855  zf_val = None
3856  # set if zf is tested (cmps, scas)
3857  for e in instr_ir: # +[updt_c]:
3858  if e.dst == zf:
3859  zf_val = e.src
3860 
3861  cond_dec = m2_expr.ExprCond(c_reg - m2_expr.ExprInt_from(c_reg, 1),
3862  m2_expr.ExprInt1(0), m2_expr.ExprInt1(1))
3863  # end condition
3864  if zf_val is None:
3865  c_cond = cond_dec
3866  elif instr.additional_info.g1.value & 2: # REPNE
3867  c_cond = cond_dec | zf
3868  elif instr.additional_info.g1.value & 4: # REP
3869  c_cond = cond_dec | (zf ^ m2_expr.ExprInt1(1))
3870 
3871  # gen while
3872  lbl_do = m2_expr.ExprId(self.gen_label(), instr.mode)
3873  lbl_end = m2_expr.ExprId(self.gen_label(), instr.mode)
3874  lbl_skip = m2_expr.ExprId(self.get_next_label(instr), instr.mode)
3875  lbl_next = m2_expr.ExprId(self.get_next_label(instr), instr.mode)
3876 
3877  for b in extra_ir:
3878  for ir in b.irs:
3879  for i, e in enumerate(ir):
3880  src = e.src.replace_expr({lbl_next: lbl_end})
3881  ir[i] = m2_expr.ExprAff(e.dst, src)
3882  cond_bloc = []
3883  cond_bloc.append(m2_expr.ExprAff(c_reg,
3884  c_reg - m2_expr.ExprInt_from(c_reg,
3885  1)))
3886  cond_bloc.append(m2_expr.ExprAff(self.IRDst, m2_expr.ExprCond(c_cond,
3887  lbl_skip,
3888  lbl_do)))
3889  cond_bloc = irbloc(lbl_end.name, [cond_bloc])
3890  e_do = instr_ir
3891 
3892  c = irbloc(lbl_do.name, [e_do])
3893  c.except_automod = False
3894  e_n = [m2_expr.ExprAff(self.IRDst, m2_expr.ExprCond(c_reg, lbl_do,
3895  lbl_skip))]
3896  return e_n, [cond_bloc, c] + extra_ir
3897 
3898  def expr_fix_regs_for_mode(self, e, mode=64):
3899  return e.replace_expr(replace_regs[mode])
3900 
3901  def expraff_fix_regs_for_mode(self, e, mode=64):
3902  dst = self.expr_fix_regs_for_mode(e.dst, mode)
3903  src = self.expr_fix_regs_for_mode(e.src, mode)
3904  return m2_expr.ExprAff(dst, src)
3905 
3906  def irbloc_fix_regs_for_mode(self, irbloc, mode=64):
3907  for irs in irbloc.irs:
3908  for i, e in enumerate(irs):
3909  """
3910  special case for 64 bits:
3911  if destination is a 32 bit reg, zero extend the 64 bit reg
3912  """
3913  if mode == 64:
3914  if (isinstance(e.dst, m2_expr.ExprId) and \
3915  e.dst.size == 32 and \
3916  e.dst in replace_regs[64]):
3917  src = self.expr_fix_regs_for_mode(e.src, mode)
3918  dst = replace_regs[64][e.dst].arg
3919  e = m2_expr.ExprAff(dst, src.zeroExtend(64))
3920  irs[i] = self.expr_fix_regs_for_mode(e, mode)
3921  irbloc.dst = self.expr_fix_regs_for_mode(irbloc.dst, mode)
3922 
3923 
3925 
3926  def __init__(self, symbol_pool=None):
3927  ir.__init__(self, mn_x86, 32, symbol_pool)
3928  self.do_stk_segm = False
3929  self.do_ds_segm = False
3930  self.do_str_segm = False
3931  self.do_all_segm = False
3932  self.pc = EIP
3933  self.sp = ESP
3934  self.IRDst = m2_expr.ExprId('IRDst', 32)
3935 
3936 
3938 
3939  def __init__(self, symbol_pool=None):
3940  ir.__init__(self, mn_x86, 64, symbol_pool)
3941  self.do_stk_segm = False
3942  self.do_ds_segm = False
3943  self.do_str_segm = False
3944  self.do_all_segm = False
3945  self.pc = RIP
3946  self.sp = RSP
3947  self.IRDst = m2_expr.ExprId('IRDst', 64)
3948 
3949  def mod_pc(self, instr, instr_ir, extra_ir):
3950  # fix RIP for 64 bit
3951  for i, expr in enumerate(instr_ir):
3952  dst, src = expr.dst, expr.src
3953  if dst != self.pc:
3954  dst = dst.replace_expr(
3955  {self.pc: m2_expr.ExprInt64(instr.offset + instr.l)})
3956  src = src.replace_expr(
3957  {self.pc: m2_expr.ExprInt64(instr.offset + instr.l)})
3958  instr_ir[i] = m2_expr.ExprAff(dst, src)
3959  for b in extra_ir:
3960  for irs in b.irs:
3961  for i, expr in enumerate(irs):
3962  dst, src = expr.dst, expr.src
3963  if dst != self.pc:
3964  new_pc = m2_expr.ExprInt64(instr.offset + instr.l)
3965  dst = dst.replace_expr({self.pc: new_pc})
3966  src = src.replace_expr(
3967  {self.pc: m2_expr.ExprInt64(instr.offset + instr.l)})
3968  irs[i] = m2_expr.ExprAff(dst, src)
def update_flag_logic
Definition: sem.py:91
def vec_op_clip
MMX/SSE/AVX operations.
Definition: sem.py:3103
def update_flag_add_cf
Definition: sem.py:118
def float_vec_vertical_instr
Definition: sem.py:3164
def update_flag_pf
Definition: sem.py:70
def vec_vertical_sem
Definition: sem.py:3117
def __vec_vertical_instr_gen
Definition: sem.py:3151
def pand
Logical (floating-point)
Definition: sem.py:3213
def set_float_cs_eip
Definition: sem.py:159
def update_flag_add
Definition: sem.py:143
def update_flag_nf
Definition: sem.py:66
def check_ops_msb
Definition: sem.py:105
def float_implicit_st0
Definition: sem.py:179
def update_flag_sub_cf
Definition: sem.py:130
def update_flag_sub_of
Definition: sem.py:136
def update_flag_sub
Definition: sem.py:152
def update_flag_znp
Definition: sem.py:83
def update_flag_zf
Definition: sem.py:61
def movsd_dispatch
Definition: sem.py:1727
def float_vec_vertical_sem
Definition: sem.py:3131
def update_flag_arith
Definition: sem.py:99
def vec_vertical_instr
Definition: sem.py:3161
def update_flag_add_of
Definition: sem.py:124
def compose_eflag
Definition: sem.py:992
def update_flag_af
Definition: sem.py:76