-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbil.py
executable file
·187 lines (157 loc) · 4.31 KB
/
bil.py
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
#!/usr/bin/env python
"""BAP BIL Python representation"""
from .adt import *
class Exp(ADT) : pass # Abstract base for all expressions
class Load(Exp):
"Load(mem,idx,endian,size)"
@property
def mem(self) : return self.arg[0]
@property
def idx(self) : return self.arg[1]
@property
def endian(self): return self.arg[2]
@property
def size(self): return self.arg[3]
class Store(Exp):
"Store(mem,idx,val,endian,size)"
@property
def mem(self) : return self.arg[0]
@property
def idx(self) : return self.arg[1]
@property
def value(self): return self.arg[2]
@property
def endian(self): return self.arg[3]
@property
def size(self): return self.arg[4]
class BinOp(Exp):
"Abstract base for all binary operators"
@property
def lhs(self): return self.arg[0]
@property
def rhs(self): return self.arg[1]
class UnOp(Exp) : pass # Abstract base for all unary operators
class Var(Exp) :
"Var(name,type)"
@property
def name(self): return self.arg[0]
@property
def type(self): return self.arg[1]
class Int(Exp):
"Int(int,size)"
@property
def value(self): return self.arg[0]
@property
def size(self):
"word size in bits"
return self.arg[1]
class Cast(Exp) :
"Abstract base for all cast operations"
@property
def size(self): return self.arg[0]
@property
def expr(self): return self.arg[1]
class Let(Exp) :
"Let(var,val,expr)"
@property
def var(self): return self.arg[0]
@property
def value(self): return self.arg[1]
@property
def expr(self): return self.arg[2]
class Unknown(Exp):
"Unknown(string,type)"
@property
def desc(self): return self.arg[0]
@property
def type(self): return self.arg[1]
class Ite(Exp):
"Ite (cond,if_true,if_false)"
@property
def cond(self): return self.arg[0]
@property
def true(self): return self.arg[1]
@property
def false(self): return self.arg[2]
class Extract(Exp):
"Extract(hb,lb, exp)"
@property
def high_bit(self): return self.arg[0]
@property
def low_bit(self): return self.arg[1]
@property
def expr(self): return self.arg[2]
class Concat(Exp):
@property
def lhs(self): return self.arg[0]
@property
def rhs(self): return self.arg[1]
class Stmt(ADT) : pass # Abstract base for all statements
class Move(Stmt) :
"Move(var,exp)"
@property
def var(self): return self.arg[0]
@property
def expr(self): return self.arg[1]
class Jmp(Stmt) : pass # Jmp(exp)
class Special(Stmt): pass # Special (string)
class While(Stmt) :
"While (cond, stmts)"
@property
def cond(self): return self.arg[0]
@property
def stmts(self): return self.arg[1]
class If(Stmt) :
"If(cond, yes-exprs, no-exprs)"
@property
def cond(self): return self.arg[0]
@property
def true(self): return self.arg[1]
@property
def false(self): return self.arg[2]
class CpuExn(Stmt) : pass # CpuExn(n)
# All BinOps have two operands of type exp
class PLUS (BinOp) : pass
class MINUS (BinOp) : pass
class TIMES (BinOp) : pass
class DIVIDE (BinOp) : pass
class SDIVIDE (BinOp) : pass
class MOD (BinOp) : pass
class SMOD (BinOp) : pass
class LSHIFT (BinOp) : pass
class RSHIFT (BinOp) : pass
class ARSHIFT (BinOp) : pass
class AND (BinOp) : pass
class OR (BinOp) : pass
class XOR (BinOp) : pass
class EQ (BinOp) : pass
class NEQ (BinOp) : pass
class LT (BinOp) : pass
class LE (BinOp) : pass
class SLT (BinOp) : pass
class SLE (BinOp) : pass
# All UnOps have one operand of type exp
class NEG (UnOp) : pass
class NOT (UnOp) : pass
# All Casts have two operands: (Int(size),exp)
class UNSIGNED(Cast) : pass
class SIGNED(Cast) : pass
class HIGH(Cast) : pass
class LOW(Cast) : pass
# Endians doesn't have values
class Endian(ADT) : pass
class LittleEndian(Endian) : pass
class BigEndian(Endian) : pass
class Type(ADT) : pass # Abstract base for expression type
class Imm(Type) :
"Imm(size) - immediate value"
@property
def size(self): return self.arg
class Mem(Type) :
"Mem(addr_size, value_size)"
@property
def addr_size(self): return self.arg[0]
@property
def value_size(self): return self.arg[1]
def loads(s):
return eval(s)