9 EXCEPT_PRIV_INSN = (1 << 17)
44 if not a
or not b
or not c
or a != b
or a != c:
45 raise ValueError(
'bad ops size %s %s %s' % (a, b, c))
49 a_s, b_s, c_s = a.size, b.size, c.size
51 a_s, b_s, c_s = a.msb(), b.msb(), c.msb()
57 "Compute cf in @res = @op1 + @op2"
58 return ExprAff(cf, (((op1 ^ op2) ^ res) ^ ((op1 ^ res) & (~(op1 ^ op2)))).msb())
62 "Compute of in @res = @op1 + @op2"
63 return ExprAff(of, (((op1 ^ res) & (~(op1 ^ op2)))).msb())
68 "Compote CF in @res = @op1 - @op2"
70 ((((op1 ^ op2) ^ res) ^ ((op1 ^ res) & (op1 ^ op2))).msb()) ^
ExprInt1(1))
74 "Compote OF in @res = @op1 - @op2"
75 return ExprAff(of, (((op1 ^ res) & (op1 ^ op2))).msb())
104 def adc(ir, instr, a, b, c=None):
108 r = b + c + cf.zeroExtend(32)
109 if instr.name ==
'ADCS' and a != PC:
119 def add(ir, instr, a, b, c=None):
124 if instr.name ==
'ADDS' and a != PC:
139 if instr.name ==
'ANDS' and a != PC:
148 def sub(ir, instr, a, b, c=None):
160 def subs(ir, instr, a, b, c=None):
174 def eor(ir, instr, a, b, c=None):
186 def eors(ir, instr, a, b, c=None):
199 def rsb(ir, instr, a, b, c=None):
211 def rsbs(ir, instr, a, b, c=None):
225 def sbc(ir, instr, a, b, c=None):
229 r = (b + cf.zeroExtend(32)) - (c +
ExprInt32(1))
237 def sbcs(ir, instr, a, b, c=None):
241 r = (b + cf.zeroExtend(32)) - (c +
ExprInt32(1))
251 def rsc(ir, instr, a, b, c=None):
255 r = (c + cf.zeroExtend(32)) - (b +
ExprInt32(1))
263 def rscs(ir, instr, a, b, c=None):
267 r = (c + cf.zeroExtend(32)) - (b +
ExprInt32(1))
278 def tst(ir, instr, a, b, c=None):
287 def teq(ir, instr, a, b, c=None):
306 def cmn(ir, instr, a, b, c=None):
316 def orr(ir, instr, a, b, c=None):
328 def orrs(ir, instr, a, b, c=None):
403 def bic(ir, instr, a, b, c=None):
407 r = b & (c ^
ExprInt(uint32(-1)))
415 def bics(ir, instr, a, b, c=None):
419 r = b & (c ^
ExprInt(uint32(-1)))
428 def mla(ir, instr, a, b, c, d):
438 def mlas(ir, instr, a, b, c, d):
449 def mul(ir, instr, a, b, c = None):
461 def muls(ir, instr, a, b, c = None):
475 r = c.zeroExtend(64) * d.zeroExtend(64)
483 r = c.zeroExtend(64) * d.zeroExtend(64) +
ExprCompose([(a, 0, 32), (b, 32, 64)])
491 r = c.signExtend(64) * d.signExtend(64)
499 r = c.signExtend(64) * d.signExtend(64) +
ExprCompose([(a, 0, 32), (b, 32, 64)])
512 def bl(ir, instr, a):
521 def bx(ir, instr, a):
537 def st_ld_r(ir, instr, a, b, store=False, size=32, s_ext=False, z_ext=False):
543 if isinstance(b, ExprOp):
547 if b.op ==
"postinc":
549 if isinstance(b, ExprOp)
and b.op
in [
"postinc",
'preinc']:
551 base, off = b.args[0], b.args[1]
566 m =
ExprMem(ad, size=size).signExtend(a.size)
568 m =
ExprMem(ad, size=size).zeroExtend(a.size)
570 raise ValueError(
'unhandled case')
577 a2 = ir.arch.regs.all_regs_ids[ir.arch.regs.all_regs_ids.index(a) + 1]
580 raise ValueError(
'the size DOES matter')
597 e.append(
ExprAff(base, base + off))
602 return st_ld_r(ir, instr, a, b, store=
False)
606 e =
st_ld_r(ir, instr, a, b, store=
False, size=64)
611 return st_ld_r(ir, instr, a, b, store=
True)
615 e =
st_ld_r(ir, instr, a, b, store=
True, size=64)
620 e =
st_ld_r(ir, instr, a, b, store=
False, size=8, z_ext=
True)
625 ir, instr, a, b, store=
False, size=8, s_ext=
True, z_ext=
False)
629 e =
st_ld_r(ir, instr, a, b, store=
True, size=8)
634 e =
st_ld_r(ir, instr, a, b, store=
False, size=16, z_ext=
True)
639 e =
st_ld_r(ir, instr, a, b, store=
True, size=16, z_ext=
True)
645 ir, instr, a, b, store=
False, size=16, s_ext=
True, z_ext=
False)
649 def st_ld_m(ir, instr, a, b, store=False, postinc=False, updown=False):
654 if isinstance(a, ExprOp)
and a.op ==
'wback':
657 if isinstance(b, ExprOp)
and b.op ==
'sbit':
671 for i, r
in enumerate(regs):
688 assert(isinstance(b, ExprOp)
and b.op ==
"reglist")
694 return st_ld_m(ir, instr, a, b, store=
False, postinc=
True, updown=
True)
698 return st_ld_m(ir, instr, a, b, store=
False, postinc=
False, updown=
True)
702 return st_ld_m(ir, instr, a, b, store=
False, postinc=
True, updown=
False)
706 return st_ld_m(ir, instr, a, b, store=
False, postinc=
False, updown=
False)
710 return st_ld_m(ir, instr, a, b, store=
True, postinc=
True, updown=
True)
714 return st_ld_m(ir, instr, a, b, store=
True, postinc=
False, updown=
True)
718 return st_ld_m(ir, instr, a, b, store=
True, postinc=
True, updown=
False)
722 return st_ld_m(ir, instr, a, b, store=
True, postinc=
False, updown=
False)
738 def lsr(ir, instr, a, b, c = None):
750 def lsrs(ir, instr, a, b, c = None):
762 def asr(ir, instr, a, b, c=None):
785 def lsl(ir, instr, a, b, c = None):
797 def lsls(ir, instr, a, b, c = None):
813 for i
in xrange(len(regs)):
825 for i
in xrange(len(regs)):
833 e.append(
ExprAff(ir.IRDst, dst))
839 lbl_next =
ExprId(ir.get_next_label(instr), 32)
846 lbl_next =
ExprId(ir.get_next_label(instr), 32)
847 e.append(ir.IRDst,
ExprCond(a, b, lbl_next))
854 r = b[:8].zeroExtend(32)
864 r = b[:16].zeroExtend(32)
874 r = b[:8].signExtend(32)
884 r = b[:16].signExtend(32)
893 def ubfx(ir, instr, a, b, c, d):
897 r = b[c:c+d].zeroExtend(32)
905 def bfc(ir, instr, a, b, c):
908 stop = start + int(c.arg)
912 out.append((a[:start], 0, start))
915 out.append((
ExprInt32(0)[last:stop], last, stop))
918 out.append((a[last:], last, 32))
975 tab_cond = {COND_EQ: zf,
996 COND_LE: zf | (nf ^ of),
1001 all_pc = ir.mn.pc.values()
1003 if ir.dst
in all_pc:
1011 if not cond
in tab_cond:
1012 raise ValueError(
'unknown condition %r' % cond)
1013 cond = tab_cond[cond]
1015 lbl_next =
ExprId(ir.get_next_label(instr), 32)
1016 lbl_do =
ExprId(ir.gen_label(), 32)
1018 dst_cond =
ExprCond(cond, lbl_do, lbl_next)
1019 assert(isinstance(instr_ir, list))
1023 if e.dst == ir.IRDst:
1027 instr_ir.append(
ExprAff(ir.IRDst, lbl_next))
1028 e_do =
irbloc(lbl_do.name, [instr_ir])
1029 e = [
ExprAff(ir.IRDst, dst_cond)]
1033 mnemo_func_cond = {}
1034 mnemo_condm0 = {
'add': add,
1082 mnemo_condm1 = {
'adds': add,
1107 mnemo_condm2 = {
'ldmia': ldmia,
1130 mnemo_nocond = {
'lsr': lsr,
1141 mn_cond_x = [mnemo_condm0,
1145 for index, mn_base
in enumerate(mn_cond_x):
1146 for mn, mf
in mn_base.items():
1147 for cond, cn
in cond_dct.items():
1154 mn_mod = mn[:-index] + cn + mn[-index:]
1156 mnemo_func_cond[mn_mod] = cond, mf
1158 for name, mf
in mnemo_nocond.items():
1159 mnemo_func_cond[name] = COND_AL, mf
1175 if not instr.name.lower()
in mnemo_func_cond:
1176 raise ValueError(
'unknown mnemo %s' % instr)
1177 cond, mf = mnemo_func_cond[instr.name.lower()]
1178 instr_ir = mf(ir, instr, *args)
1180 return instr, extra_ir
1182 get_arm_instr_expr = get_mnemo_expr
1192 ir.__init__(self, mn_arm,
"l", symbol_pool)
1200 if len(args)
and isinstance(args[-1], ExprOp):
1201 if args[-1].op ==
'rrx':
1203 [(args[-1].args[0][1:], 0, 31), (cf, 31, 32)])
1204 elif (args[-1].op
in [
'<<',
'>>',
'<<a',
'a>>',
'<<<',
'>>>']
and
1205 isinstance(args[-1].args[-1], ExprId)):
1206 args[-1] =
ExprOp(args[-1].op,
1208 args[-1].args[-1][:8].zeroExtend(32))
1212 for i, x
in enumerate(instr_ir):
1213 x =
ExprAff(x.dst, x.src.replace_expr(
1218 for i, x
in enumerate(irs):
1219 x =
ExprAff(x.dst, x.src.replace_expr(
1223 return instr_ir, extra_ir
1228 ir.__init__(self, mn_arm,
"b", symbol_pool)
1235 ir.__init__(self, mn_armt,
"l", symbol_pool)
1245 ir.__init__(self, mn_armt,
"b", symbol_pool)