about summary refs log tree commit diff stats
path: root/miasm/expression/expression_helper.py
blob: 8cf422bbb0451e216cedea12ef0d551058e49dab (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#
# Copyright (C) 2011 EADS France, Fabrice Desclaux <fabrice.desclaux@eads.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from miasm.expression.expression import *

tab_size_int = {8:uint8,
                16:uint16,
                32:uint32,
                64:uint64,
                }

tab_max_uint = {8:uint8(0xFF), 16:uint16(0xFFFF), 32:uint32(0xFFFFFFFF), 64:uint64(0xFFFFFFFFFFFFFFFFL)}


def parity(a):
    tmp = (a)&0xFFL
    cpt = 1
    while tmp!=0:
        cpt^=tmp&1
        tmp>>=1
    return cpt

def merge_sliceto_slice(args):
    sources = {}
    non_slice = {}
    sources_int = {}
    for a in args:
        if isinstance(a[0], ExprInt):
            #sources_int[a.start] = a
            # copy ExprInt because we will inplace modify arg just below
            # /!\ TODO XXX never ever modify inplace args...
            sources_int[a[1]] = (ExprInt(a[0].arg.__class__(a[0].arg)),
                                 a[1],
                                 a[2])
        elif isinstance(a[0], ExprSlice):
            if not a[0].arg in sources:
                sources[a[0].arg] = []
            sources[a[0].arg].append(a)
        else:
            non_slice[a[1]] = a


    #find max stop to determine size
    max_size = None
    for a in args:
        if max_size == None or max_size < a[2]:
            max_size = a[2]

    #first simplify all num slices
    final_sources = []
    sorted_s = []
    for x in sources_int.values():
        #mask int
        v = x[0].arg & ((1<<(x[2]-x[1]))-1)
        x[0].arg = v
        sorted_s.append((x[1], x))
    sorted_s.sort()

    while sorted_s:
        start, v = sorted_s.pop()
        out = e.reload_expr()
        while sorted_s:
            if sorted_s[-1][1][2] != start:
                break

            start = sorted_s[-1][1][1]

            a = uint64((int(out[0].arg) << (out[1] - start )) + sorted_s[-1][1][0].arg)
            out.arg = ExprInt(uint32(a))
            sorted_s.pop()
            out[1] = start

        out_type = tab_size_int[max_size]
        out[0].arg = out_type(out[0].arg)
        final_sources.append((start, out))

    final_sources_int = final_sources

    #check if same sources have corresponding start/stop
    #is slice AND is sliceto
    simp_sources = []
    for s, args in sources.items():
        final_sources = []
        sorted_s = []
        for x in args:
            sorted_s.append((x[1], x))
        sorted_s.sort()
        while sorted_s:
            start, v = sorted_s.pop()
            out = v[0].reload_expr(), v[1], v[2]
            while sorted_s:
                if sorted_s[-1][1][2] != start:
                    break
                if sorted_s[-1][1][0].stop != out[0].start:
                    break

                start = sorted_s[-1][1][1]
                out[0].start = sorted_s[-1][1][0].start
                sorted_s.pop()
            out = out[0], start, out[2]

            final_sources.append((start, out))

        simp_sources+=final_sources

    simp_sources+= final_sources_int

    for i, v in non_slice.items():
        simp_sources.append((i, v))

    simp_sources.sort()

    simp_sources = [x[1] for x in simp_sources]
    return simp_sources


def expr_simp(e):
    if e.is_simp:
        return e
    e = expr_simp_w(e)
    e.is_simp = True
    return e

def expr_simp_w(e):
    if isinstance(e, ExprTop):
        return e
    if isinstance(e, ExprInt):
        return e
    elif isinstance(e, ExprId):
        return e
    elif isinstance(e, ExprAff):
        return ExprAff(expr_simp(e.dst), expr_simp(e.src))
    elif isinstance(e, ExprCond):
        c = expr_simp(e.cond)
        if isinstance(c, ExprInt):
            print e
            fdsfsdf
            if c == 0:
                return expr_simp(e.src2)
            else:
                return expr_simp(e.src1)

        return ExprCond(expr_simp(e.cond), expr_simp(e.src1), expr_simp(e.src2))
    elif isinstance(e, ExprMem):
        if isinstance(e.arg, ExprTop):
            return ExprTop()
        return ExprMem(expr_simp(e.arg), size = e.size)
    elif isinstance(e, ExprOp):
        op, args = e.op, list(e.args)
        """
        if ExprTop() in args:
            return ExprTop()
        """
        #int OP int => int
        if e.op in ['+', '-', '*', '|', '&', '^', '>>', '<<'] and isinstance(args[0], ExprInt) and isinstance(args[1], ExprInt) :
            if args[0].get_size() != args[1].get_size():
                raise ValueError("diff size! %s"%(str(e)))
            if e.op == '+':
                o = args[0].arg + args[1].arg
            elif e.op == '-':
                o = args[0].arg - args[1].arg
            elif e.op == '*':
                o = args[0].arg * args[1].arg
            elif e.op == '|':
                o = args[0].arg | args[1].arg
            elif e.op == '&':
                o = args[0].arg & args[1].arg
            elif e.op == '^':
                o = args[0].arg ^ args[1].arg
            elif e.op == '>>':
                o = args[0].arg >> args[1].arg
            elif e.op == '<<':
                o = args[0].arg << args[1].arg
            else:
                raise ValueError("zarb op %s"%str(e))
            z = ExprInt(tab_size_int[args[0].get_size()](o))
            return z

        #int OP xx => xx OP int
        if e.op in ['+', '*', '|', '&', '^']:
            if isinstance(e.args[0], ExprInt) and not isinstance(e.args[1], ExprInt):
                op, args= e.op, [e.args[1], e.args[0]]
        #A+0 =>A
        if op in ['+', '-', '|', "^", "<<", ">>"]:
            if isinstance(args[0], ExprInt) and args[0].arg == 0 and not op in ['-', "<<", ">>"]:
                return expr_simp(args[1])
            if isinstance(args[1], ExprInt) and args[1].arg == 0:
                return expr_simp(args[0])

        #A&0 =>0
        if op in ['&']:
            if isinstance(args[1], ExprInt) and args[1].arg == 0:
                return args[1]

        #A-(-123) =>A+123
        if op == '-' and isinstance(args[1], ExprInt) and int32(args[1].arg)<0 :
            op = '+'
            args[1] = ExprInt(-args[1].arg)

        #A+(-123) =>A-123
        if op == '+' and isinstance(args[1], ExprInt) and int32(args[1].arg)<0 :
            op = '-'
            args[1] = ExprInt(-args[1].arg)
            #fdsfs
        #A+3+2 => A+5
        if op in ['+', '-'] and isinstance(args[1], ExprInt) and isinstance(args[0], ExprOp) and args[0].op in ['+', '-'] and isinstance(args[0].args[1], ExprInt):
            op1 = op
            op2 = args[0].op
            if op1 == op2:
                op = op1
                args1 = args[0].args[1].arg + args[1].arg
            else:
                op = op2
                args1 = args[0].args[1].arg - args[1].arg


                #if op == '-':
                #    args1 = -args1
            args0 = args[0].args[0]
            args = [args0, ExprInt(args1)]

        if op in ['+'] and isinstance(args[1], ExprInt) and isinstance(args[0], ExprOp) and args[0].op in ['+', '-'] and isinstance(args[0].args[0], ExprInt):
            op = args[0].op
            args1 = args[0].args[0].arg + args[1].arg
            args0 = args[0].args[1]
            args = [ExprInt(args1), args0]

        #0 - (a-b) => b-a
        if op == '-' and isinstance(args[0], ExprInt) and args[0].arg == 0 and isinstance(args[1], ExprOp) and args[1].op == "-":
            return expr_simp(args[1].args[1] - args[1].args[0])

        #a<<< x <<< y => a <<< (x+y) (ou <<< >>>)
        if op in ['<<<', '>>>'] and isinstance(args[1], ExprInt) and isinstance(args[0], ExprOp) and args[0].op in ['<<<', '>>>'] and isinstance(args[0].args[1], ExprInt):
            op1 = op
            op2 = args[0].op
            if op1 == op2:
                op = op1
                args1 = args[0].args[1].arg + args[1].arg
            else:
                op = op2
                args1 = args[0].args[1].arg - args[1].arg

            args0 = args[0].args[0]
            args = [args0, ExprInt(args1)]


        #a >>> 0 => a (ou <<<)
        if op in ['<<<', '>>>'] and isinstance(args[1], ExprInt) and args[1].arg == 0:
            e = expr_simp(args[0])
            return e

        #((a >>> b) <<< b) => a
        if op in ['<<<', '>>>'] and isinstance(args[0], ExprOp) and args[0].op in ['<<<', '>>>'] and args[1] == args[0].args[1]:
            oo = op, args[0].op
            if oo in [('<<<', '>>>'), ('>>>', '<<<')]:

                e = expr_simp(args[0].args[0])
                return e


        #( a + int1 ) - (b+int2) => a - (b+ (int1-int2))
        if op in ['+', '-'] and isinstance(args[0], ExprOp) and args[0].op in ['+', '-'] and isinstance(args[1], ExprOp) and args[1].op in ['+', '-'] and isinstance(args[0].args[1], ExprInt) and isinstance(args[1].args[1], ExprInt):
            op1 = op
            op2 = args[0].op
            op3 = args[1].op

            if op1 == op2:
                m_op = "+"
            else:
                m_op = "-"
            e = ExprOp(op1,
                       args[0].args[0],
                       ExprOp(m_op,
                              ExprOp(op3,
                                     args[1].args[0],
                                     args[1].args[1]
                                     ),
                              args[0].args[1]
                              )
                       )
            e = expr_simp(e)

            return e

        #(a - (a + XXX)) => 0-XXX
        if op in ['-'] and isinstance(args[1], ExprOp) and args[1].op in ['+', '-'] and args[1].args[0] == args[0]:
            if op == args[1].op:
                m_op = "+"
            else:
                m_op = "-"

            z = ExprInt(tab_size_int[args[1].args[1].get_size()](0))
            e = ExprOp(m_op,
                       z,
                       args[1].args[1])
            e = expr_simp(e)

            return e


        #((a +- XXX) -a) => 0+-XXX
        if op in ['-'] and isinstance(args[0], ExprOp) and args[0].op in ['+', '-'] and args[0].args[0] == args[1]:
            m_op = args[0].op

            z = ExprInt(tab_size_int[args[0].args[1].get_size()](0))
            e = ExprOp(m_op,
                       z,
                       args[0].args[1])
            e = expr_simp(e)

            return e

        #  ((a ^ b) ^ a) => b (or commut)
        if op in ['^'] and isinstance(args[0], ExprOp) and args[0].op in ['^']:
            rest_a = None
            if args[0].args[0] == args[1]:
                rest_a = args[0].args[1]
            elif args[0].args[1] == args[1]:
                rest_a = args[0].args[0]
            if rest_a != None:
                e = expr_simp(rest_a)
                return e
        #  (a ^ (a ^ b) ) => b (or commut)
        if op in ['^'] and isinstance(args[1], ExprOp) and args[1].op in ['^']:
            rest_a = None
            if args[1].args[0] == args[0]:
                rest_a = args[1].args[1]
            elif args[1].args[1] == args[0]:
                rest_a = args[1].args[0]
            if rest_a != None:
                e = expr_simp(rest_a)
                return e


        #  ((a + b) - b) => a (or commut)
        if op in ['-'] and isinstance(args[0], ExprOp) and args[0].op in ['+']:
            rest_a = None
            if args[0].args[1] == args[1]:
                rest_a = args[0].args[0]
                e = expr_simp(rest_a)
                return e

        #  ((a - b) + b) => a (or commut)
        if op in ['+'] and isinstance(args[0], ExprOp) and args[0].op in ['-']:
            rest_a = None
            if args[0].args[1] == args[1]:
                rest_a = args[0].args[0]
                e = expr_simp(rest_a)
                return e

        # a<<< a.size => a
        if op in ['<<<', '>>>'] and isinstance(args[1], ExprInt) and args[1].arg == args[0].get_size():
            return expr_simp(args[0])

        #!!a => a
        if op == '!' and isinstance(args[0], ExprOp) and args[0].op == '!':
            new_e = args[0].args[0]
            return expr_simp(new_e)

        #! (!X + int) => X - int
        if op == '!' and isinstance(args[0], ExprOp) and args[0].op in ['+', '-'] and isinstance(args[0].args[0], ExprOp) and args[0].args[0].op == '!':
            if args[0].op == '+':
                op = '-'
            else:
                op = '+'
            return expr_simp(ExprOp(op, args[0].args[0].args[0], args[0].args[1]))

        # ((a (op1+-) int)  (op2+-) b) => ((a (op2) b) op1 int))
        if op in ['+', '-'] and isinstance(args[0], ExprOp) and args[0].op in ['+', '-'] and not isinstance(args[1], ExprInt) and args[0].op in ['+', '-'] and isinstance(args[0].args[1], ExprInt):
            op1 = op
            op2 = args[0].op
            e = ExprOp(op2,
                       ExprOp(op1,
                              args[0].args[0],
                              args[1])
                       ,
                       args[0].args[1])
            return expr_simp(e)


        if op == "&" and isinstance(args[0], ExprOp) and args[0].op == '!' and isinstance(args[1], ExprOp) and args[1].op == '!' and isinstance(args[0].args[0], ExprOp) and args[0].args[0].op == '&' and isinstance(args[1].args[0], ExprOp) and args[1].args[0].op == '&':

            ##############1
            a1 = args[0].args[0].args[0]
            if isinstance(a1, ExprOp) and a1.op == '!':
                a1 = a1.args[0]
            elif isinstance(a1, ExprInt):
                a1 = ExprInt(~a1.arg)
            else:
                a1 = None

            b1 = args[0].args[0].args[1]
            if isinstance(b1, ExprOp) and b1.op == '!':
                b1 = b1.args[0]
            elif isinstance(b1, ExprInt):
                b1 = ExprInt(~b1.arg)
            else:
                b1 = None


            a2 = args[1].args[0].args[0]
            b2 = args[1].args[0].args[1]


            if a1 != None and b1 != None and a1 == a2 and b1 == b2:
                new_e = ExprOp('^', a1, b1)
                return expr_simp(new_e)

            ################2
            a1 = args[1].args[0].args[0]
            if isinstance(a1, ExprOp) and a1.op == '!':
                a1 = a1.args[0]
            elif isinstance(a1, ExprInt):
                a1 = ExprInt(~a1.arg)
            else:
                a1 = None

            b1 = args[1].args[0].args[1]
            if isinstance(b1, ExprOp) and b1.op == '!':
                b1 = b1.args[0]
            elif isinstance(b1, ExprInt):
                b1 = ExprInt(~b1.arg)
            else:
                b1 = None


            a2 = args[0].args[0].args[0]
            b2 = args[0].args[0].args[1]


            if a1 != None and b1 != None and a1 == a2 and b1 == b2:
                new_e = ExprOp('^', a1, b1)
                return expr_simp(new_e)


        # (x & mask) >> shift whith mask < 2**shift => 0
        if op == ">>" and isinstance(args[1], ExprInt) and isinstance(args[0], ExprOp) and args[0].op == "&":
            if isinstance(args[0].args[1], ExprInt) and 2**args[1].arg >= args[0].args[1].arg:
                return ExprInt(tab_size_int[args[0].get_size()](0))

        #! (compose a b c) => (compose !a !b !c)
        if op == '!' and isinstance(args[0], ExprCompose):
            args = [(ExprOp('!', x.arg), x[1], x[2]) for x in args[0].args]
            new_e = ExprCompose(args)
            return expr_simp(new_e)
        #!a[0:X] => (!a)[0:X]
        if op == '!' and isinstance(args[0], ExprSlice):
            new_e = ExprSlice(ExprOp('!', args[0].arg), args[0].start, args[0].stop)
            return expr_simp(new_e)


        #! int
        if op == '!' and isinstance(args[0], ExprInt):
            a = args[0]
            e = ExprInt(tab_max_uint[a.get_size()]^a.arg)
            return e

        #a^a=>0 | a-a =>0
        if op in ['^', '-'] and args[0] == args[1]:
            tmp =  ExprInt(tab_size_int[args[0].get_size()](0))
            return tmp

        #a & a => a   or a | a => a
        if op in ['&', '|'] and args[0] == args[1]:
            return expr_simp(args[0])
        # int == int => 0 or 1
        if op == '==' and isinstance(args[0], ExprInt) and isinstance(args[1], ExprInt):
            if args[0].arg == args[1].arg:
                return ExprInt(tab_size_int[args[0].get_size()](1))
            else:
                return ExprInt(tab_size_int[args[0].get_size()](0))
        #( a|int == 0)  => 0  wirh int != 0
        if op == '==' and isinstance(args[1], ExprInt) and args[1].arg ==0 :
            if isinstance(args[0], ExprOp) and args[0].op == '|' and isinstance(args[0].args[1], ExprInt) and \
               args[0].args[1].arg != 0:
                return ExprInt(tab_size_int[args[0].get_size()](0))


        if op == 'parity' and isinstance(args[0], ExprInt):
            return ExprInt(tab_size_int[args[0].get_size()](parity(args[0].arg)))

        new_e = ExprOp(op, *[expr_simp(x) for x in args])
        if new_e == e:
            return new_e
        else:
            return expr_simp(new_e)

    #Slice(int) => new_int
    elif isinstance(e, ExprSlice):
        arg = expr_simp(e.arg)

        if isinstance(arg, ExprTop):
            return ExprTop()
        elif e.start == 0 and e.stop == 32 and arg.get_size() == 32:
            return arg

        elif isinstance(arg, ExprInt):
            total_bit = e.stop-e.start
            mask = uint64((uint64(1)<<(e.stop-e.start))-1)
            if total_bit in tab_size_int:
                return ExprInt(tab_size_int[total_bit]((uint64((arg.arg)>>e.start)) & mask))
            else:
                return ExprInt(type(arg.arg)((uint64((arg.arg)>>e.start)) & mask))
        elif isinstance(arg, ExprSlice):
            if e.stop-e.start > arg.stop-arg.start:
                raise ValueError('slice in slice: getting more val', str(e))

            new_e = ExprSlice(expr_simp(arg.arg), e.start + arg.start, e.start + arg.start + (e.stop - e.start))
            return expr_simp(new_e)
        elif isinstance(arg, ExprCompose):
            for a in arg.args:
                if a[1] <= e.start and a[2]>=e.stop:
                    new_e = a[0][e.start-a[1]:e.stop-a[1]]
                    new_e = expr_simp(new_e)
                    return new_e
        elif isinstance(arg, ExprOp) and e.start == 0:
            #if (op ..)[0:X] and op result is good size, skip slice
            if e.stop == arg.get_size():
                return expr_simp(arg)
            return ExprSlice(arg, e.start, e.stop)
        elif isinstance(arg, ExprMem) and e.start == 0 and arg.size == e.stop:
            e = expr_simp(arg)
            return e
        #XXXX todo hum, is it safe?
        elif isinstance(arg, ExprMem) and e.start == 0 and arg.size > e.stop and e.stop %8 == 0:
            e = expr_simp(ExprMem(e.arg.arg, size = e.stop))
            return e




        return ExprSlice(arg, e.start, e.stop)
        """
    XXX todo move to exprcompose
    elif isinstance(e, ExprSliceTo):
        if isinstance(e.arg, ExprTop):
            return ExprTop()
        if isinstance(e.arg, ExprSlice) and e.arg.start == 0:
            return expr_simp(ExprSliceTo(expr_simp(e.arg.arg), e.start, e.stop))

        #(.., a[0:X], ..) _to[Y:Z] with X > Z-Y => a[0:X]_to[Y:Z]
        if isinstance(e.arg, ExprCompose) and len(e.arg.args) >1:
            s = e.get_size()
            for a in e.arg.args:
                if a.start == 0 and a.stop >= s:
                    return expr_simp(ExprSliceTo(ExprCompose([a]), e.start, e.stop))



        return ExprSliceTo(expr_simp(e.arg), e.start, e.stop)
        """
    elif isinstance(e, ExprCompose):
        #(.., a_to[x:y], a[:]_to[y:z], ..) => (.., a[x:z], ..)
        e = ExprCompose([(expr_simp(x[0]), x[1], x[2]) for x in e.args])
        args = []
        i = -1
        simp = False
        while i+1 < len(e.args):
            i+=1
            if not args:
                args.append(e.args[i])
                continue
            if args[-1][2] != e.args[i][1]:
                continue
            if not isinstance(e.args[i][0], ExprSlice):
                continue
            if isinstance(args[-1][0], ExprSlice):
                a = args[-1]
            else:
                a = (ExprSlice(args[-1][0], 0, args[-1][0].get_size()),
                     args[-1][1],
                     args[-1][2])
            if a[0].arg != e.args[i][0].arg:
                continue
            if a[2] != e.args[i][1]:
                continue
            args[-1] = (e.args[i][0].arg, a[1], e.args[i][2])
            simp = True

        if simp:
            return expr_simp(ExprCompose(args))



        all_top = True
        for a in e.args:
            if not isinstance(a, ExprTop):
                all_top = False
                break
        if all_top:
            return ExprTop()
        """
        if ExprTop() in e.args:
            return ExprTop()
        """

        args = merge_sliceto_slice(e.args)
        if len(args) == 1:
            a = args[0]
            if isinstance(a[0], ExprInt):
                if a[0].get_size() != a[2]:
                    print a, a[0].get_size(), a[2]
                    raise ValueError("todo cast in compose!", e)
                return a[0]
            uu = expr_simp(a[0][:e.get_size()])
            return uu
        if len(args) != len(e.args):
            return expr_simp(ExprCompose(args))
        else:
            return ExprCompose(args)
    else:
        raise 'bad expr'


def expr_cmp(e1, e2):
    return str(e1) == str(e2)
"""
#replace id by another in expr
def expr_replace(e, repl):
    if isinstance(e, ExprInt):
        return e
    elif isinstance(e, ExprId):
        if e in repl:
            return repl[e]
        return e
    elif isinstance(e, ExprAff):
        return ExprAff(expr_replace(e.dst, repl), expr_replace(e.src, repl))
    elif isinstance(e, ExprCond):
        return ExprCond(expr_replace(e.cond, repl), expr_replace(e.src1, repl), expr_replace(e.src2, repl))
    elif isinstance(e, ExprMem):
        return ExprMem(expr_replace(e.arg, repl), size = e.size)
    elif isinstance(e, ExprOp):
        return ExprOp(e.op, *[expr_replace(x, repl) for x in e.args])
    elif isinstance(e, ExprSlice):
        return ExprSlice(expr_replace(e.arg, repl), e.start, e.stop)
    elif isinstance(e, ExprCompose):
        return ExprCompose([(expr_replace(x[0], repl), x[1], x[2]) for x in e.args])
    else:
        raise ValueError('bad expr', e)



"""