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

Source Code for Module bap.bil

  1  #!/usr/bin/env python 
  2   
  3  """BAP BIL Python representation""" 
  4   
  5  from .adt import * 
6 7 8 -class Exp(ADT) : pass # Abstract base for all expressions
9 -class Load(Exp):
10 "Load(mem,idx,endian,size)" 11 @property
12 - def mem(self) : return self.arg[0]
13 @property
14 - def idx(self) : return self.arg[1]
15 @property
16 - def endian(self): return self.arg[2]
17 @property
18 - def size(self): return self.arg[3]
19
20 21 -class Store(Exp):
22 "Store(mem,idx,val,endian,size)" 23 @property
24 - def mem(self) : return self.arg[0]
25 @property
26 - def idx(self) : return self.arg[1]
27 @property
28 - def value(self): return self.arg[2]
29 @property
30 - def endian(self): return self.arg[3]
31 @property
32 - def size(self): return self.arg[4]
33
34 -class BinOp(Exp):
35 "Abstract base for all binary operators" 36 @property
37 - def lhs(self): return self.arg[0]
38 @property
39 - def rhs(self): return self.arg[1]
40
41 -class UnOp(Exp) : pass # Abstract base for all unary operators
42
43 -class Var(Exp) :
44 "Var(name,type)" 45 @property
46 - def name(self): return self.arg[0]
47 @property
48 - def type(self): return self.arg[1]
49
50 -class Int(Exp):
51 "Int(int,size)" 52 @property
53 - def value(self): return self.arg[0]
54 @property
55 - def size(self):
56 "word size in bits" 57 return self.arg[1]
58
59 -class Cast(Exp) :
60 "Abstract base for all cast operations" 61 @property
62 - def size(self): return self.arg[0]
63 @property
64 - def expr(self): return self.arg[1]
65
66 -class Let(Exp) :
67 "Let(var,val,expr)" 68 @property
69 - def var(self): return self.arg[0]
70 @property
71 - def value(self): return self.arg[1]
72 @property
73 - def expr(self): return self.arg[2]
74
75 -class Unknown(Exp):
76 "Unknown(string,type)" 77 @property
78 - def desc(self): return self.arg[0]
79 @property
80 - def type(self): return self.arg[1]
81
82 -class Ite(Exp):
83 "Ite (cond,if_true,if_false)" 84 @property
85 - def cond(self): return self.arg[0]
86 @property
87 - def true(self): return self.arg[1]
88 @property
89 - def false(self): return self.arg[2]
90
91 -class Extract(Exp):
92 "Extract(hb,lb, exp)" 93 @property
94 - def high_bit(self): return self.arg[0]
95 @property
96 - def low_bit(self): return self.arg[1]
97 @property
98 - def expr(self): return self.arg[2]
99
100 -class Concat(Exp):
101 @property
102 - def lhs(self): return self.arg[0]
103 @property
104 - def rhs(self): return self.arg[1]
105
106 -class Stmt(ADT) : pass # Abstract base for all statements
107
108 -class Move(Stmt) :
109 "Move(var,exp)" 110 @property
111 - def var(self): return self.arg[0]
112 @property
113 - def expr(self): return self.arg[1]
114
115 -class Jmp(Stmt) : pass # Jmp(exp)
116 -class Special(Stmt): pass # Special (string)
117 -class While(Stmt) :
118 "While (cond, stmts)" 119 @property
120 - def cond(self): return self.arg[0]
121 122 @property
123 - def stmts(self): return self.arg[1]
124
125 -class If(Stmt) :
126 "If(cond, yes-exprs, no-exprs)" 127 @property
128 - def cond(self): return self.arg[0]
129 @property
130 - def true(self): return self.arg[1]
131 @property
132 - def false(self): return self.arg[2]
133
134 -class CpuExn(Stmt) : pass # CpuExn(n)
135
136 # All BinOps have two operands of type exp 137 -class PLUS (BinOp) : pass
138 -class MINUS (BinOp) : pass
139 -class TIMES (BinOp) : pass
140 -class DIVIDE (BinOp) : pass
141 -class SDIVIDE (BinOp) : pass
142 -class MOD (BinOp) : pass
143 -class SMOD (BinOp) : pass
144 -class LSHIFT (BinOp) : pass
145 -class RSHIFT (BinOp) : pass
146 -class ARSHIFT (BinOp) : pass
147 -class AND (BinOp) : pass
148 -class OR (BinOp) : pass
149 -class XOR (BinOp) : pass
150 -class EQ (BinOp) : pass
151 -class NEQ (BinOp) : pass
152 -class LT (BinOp) : pass
153 -class LE (BinOp) : pass
154 -class SLT (BinOp) : pass
155 -class SLE (BinOp) : pass
156
157 # All UnOps have one operand of type exp 158 -class NEG (UnOp) : pass
159 -class NOT (UnOp) : pass
160
161 # All Casts have two operands: (Int(size),exp) 162 -class UNSIGNED(Cast) : pass
163 -class SIGNED(Cast) : pass
164 -class HIGH(Cast) : pass
165 -class LOW(Cast) : pass
166
167 # Endians doesn't have values 168 -class Endian(ADT) : pass
169 -class LittleEndian(Endian) : pass
170 -class BigEndian(Endian) : pass
171
172 -class Type(ADT) : pass # Abstract base for expression type
173 -class Imm(Type) :
174 "Imm(size) - immediate value" 175 @property
176 - def size(self): return self.arg
177
178 -class Mem(Type) :
179 "Mem(addr_size, value_size)" 180 @property
181 - def addr_size(self): return self.arg[0]
182 183 @property
184 - def value_size(self): return self.arg[1]
185
186 -def loads(s):
187 return eval(s)
188