about summary refs log tree commit diff stats
path: root/miasm2/expression/stp.py
blob: c9b76e4c8971789c654bf3106e4512fee0fd5847 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from miasm2.expression.expression import *


"""
Quick implementation of miasm traduction to stp langage
TODO XXX: finish
"""


def ExprInt_strcst(self):
    b = bin(int(self))[2::][::-1]
    b += "0" * self.size
    b = b[:self.size][::-1]
    return "0bin" + b


def ExprId_strcst(self):
    return self.name


def genop(op, size, a, b):
    return op + '(' + str(size) + ',' + a + ', ' + b + ')'


def genop_nosize(op, size, a, b):
    return op + '(' + a + ', ' + b + ')'


def ExprOp_strcst(self):
    op = self.op
    op_dct = {"|": " | ",
              "&": " & "}
    if op in op_dct:
        return '(' + op_dct[op].join([x.strcst() for x in self.args]) + ')'
    op_dct = {"-": "BVUMINUS"}
    if op in op_dct:
        return op_dct[op] + '(' + self.args[0].strcst() + ')'
    op_dct = {"^": ("BVXOR", genop_nosize),
              "+": ("BVPLUS", genop)}
    if not op in op_dct:
        raise ValueError('implement op', op)
    op, f = op_dct[op]
    args = [x.strcst() for x in self.args][::-1]
    a = args.pop()
    b = args.pop()
    size = self.args[0].size
    out = f(op, size, a, b)
    while args:
        out = f(op, size, out, args.pop())
    return out


def ExprSlice_strcst(self):
    return '(' + self.arg.strcst() + ')[%d:%d]' % (self.stop - 1, self.start)


def ExprCond_strcst(self):
    cond = self.cond.strcst()
    src1 = self.src1.strcst()
    src2 = self.src2.strcst()
    return "(IF %s=(%s) THEN %s ELSE %s ENDIF)" % (
        "0bin%s" % ('0' * self.cond.size), cond, src2, src1)

ExprInt.strcst = ExprInt_strcst
ExprId.strcst = ExprId_strcst
ExprOp.strcst = ExprOp_strcst
ExprCond.strcst = ExprCond_strcst
ExprSlice.strcst = ExprSlice_strcst