Package bap :: Module asm
[hide private]
[frames] | no frames]

Source Code for Module bap.asm

 1  #!/usr/bin/env python 
 2   
 3  """Disassembled instuctions""" 
 4   
 5  from adt import ADT 
 6   
7 -class Kind(ADT) : pass
8 -class Having_side_effects(Kind) : pass
9 -class Affecting_control(Kind) : pass
10 -class Branch(Affecting_control) : pass
11 -class Conditional_branch(Branch) : pass
12 -class Unconditional_branch(Branch) : pass
13 -class Indirect_branch(Branch) : pass
14 -class Return(Affecting_control) : pass
15 -class Call(Affecting_control) : pass
16 -class Barrier(Affecting_control) : pass
17 -class Terminator(Affecting_control): pass
18 -class May_affect_control_flow(Affecting_control) : pass
19 -class May_load(Having_side_effects) : pass
20 -class May_store(Having_side_effects) : pass
21 -class Valid(Kind) : pass
22 23
24 -def eval_if_not_adt(s):
25 if isinstance(s, ADT): 26 return s 27 else: 28 return eval(s)
29 30
31 -def map_eval(ss):
32 return [eval_if_not_adt(s) for s in ss]
33 34 35
36 -class Insn(object) :
37 - def __init__(self, name, addr, size, asm, kinds, operands, target=None, bil=None, **kw):
38 self.name = name 39 self.addr = int(addr) 40 self.size = int(size) 41 self.operands = map_eval(operands) 42 self.asm = str(asm) 43 self.kinds = map_eval(kinds) 44 self.target = target 45 self.bil = bil 46 self.__dict__.update(kw)
47
48 - def has_kind(self, k):
49 return exists(self.kinds, lambda x: isinstance(x,k))
50
51 - def __repr__(self):
52 return 'Insn("{name}", {addr:#010x}, {size}, "{asm}", {kinds}, {operands})'.\ 53 format(**self.__dict__)
54
55 -class Op(ADT) : pass
56 -class Reg(Op) : pass
57 -class Imm(Op) : pass
58 -class Fmm(Op) : pass
59 60
61 -def exists(cont,f):
62 try: 63 r = (x for x in cont if f(x)).next() 64 return True 65 except StopIteration: 66 return False
67