-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathanf.lua
299 lines (282 loc) · 8.89 KB
/
anf.lua
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
module(...,package.seeall)
local utils = require('pf.utils')
local set, pp, dup = utils.set, utils.pp, utils.dup
local relops = set('<', '<=', '=', '!=', '>=', '>')
local binops = set(
'+', '-', '*', '*64', '/', '&', '|', '^', '<<', '>>'
)
local unops = set('ntohs', 'ntohl', 'uint32', 'int32')
local simple = set('true', 'false', 'match', 'fail')
local count = 0
local function fresh()
count = count + 1
return 'var'..count
end
local function lower_arith(expr, k)
if type(expr) ~= 'table' then return k(expr) end
local op = expr[1]
if unops[op] then
local operand = expr[2]
local function have_operand(operand)
local result = fresh()
return { 'let', result, { op, operand }, k(result) }
end
return lower_arith(operand, have_operand)
elseif binops[op] then
local lhs, rhs = expr[2], expr[3]
local function have_lhs(lhs)
local function have_rhs(rhs)
local result = fresh()
return { 'let', result, { op, lhs, rhs}, k(result) }
end
return lower_arith(rhs, have_rhs)
end
return lower_arith(lhs, have_lhs)
else
assert(op == '[]')
local operand, size = expr[2], expr[3]
local function have_operand(operand)
local result = fresh()
return { 'let', result, { op, operand, size }, k(result) }
end
return lower_arith(operand, have_operand)
end
end
local function lower_comparison(expr, k)
local op, lhs, rhs = expr[1], expr[2], expr[3]
assert(relops[op])
local function have_lhs(lhs)
local function have_rhs(rhs)
return k({ op, lhs, rhs })
end
return lower_arith(rhs, have_rhs)
end
return lower_arith(lhs, have_lhs)
end
local function lower_bool(expr, k)
local function lower(expr)
local function have_bool(expr)
return expr
end
return lower_bool(expr, have_bool)
end
local op = expr[1]
if op == 'if' then
local test, t, f = expr[2], expr[3], expr[4]
local function have_test(test)
return k({ 'if', test, lower(t), lower(f) })
end
return lower_bool(test, have_test)
elseif simple[op] then
return k(expr)
elseif op == 'call' then
local out = { 'call', expr[2] }
local function lower_arg(i)
if i > #expr then return k(out) end
local function have_arg(arg)
out[i] = arg
return lower_arg(i + 1)
end
return lower_arith(expr[i], have_arg)
end
return lower_arg(3)
else
return lower_comparison(expr, k)
end
end
local function lower(expr)
count = 0
local function have_bool(expr)
return expr
end
return lower_bool(expr, have_bool)
end
local function cse(expr)
local replacements = {}
local function lookup(expr)
return replacements[expr] or expr
end
local function visit(expr, env)
if type(expr) == 'number' then return expr end
if type(expr) == 'string' then return lookup(expr) end
local op = expr[1]
if op == 'let' then
local var, val, body = expr[2], expr[3], expr[4]
assert(type(val) == 'table')
local arith_op = val[1]
local key, replacement_val
if unops[arith_op] then
local lhs = visit(val[2], env)
key = arith_op..','..lhs
replacement_val = { arith_op, lhs }
elseif binops[arith_op] then
local lhs, rhs = visit(val[2], env), visit(val[3], env)
key = arith_op..','..lhs..','..rhs
replacement_val = { arith_op, lhs, rhs }
else
assert(arith_op == '[]')
local lhs, size = visit(val[2], env), val[3]
key = arith_op..','..lhs..','..size
replacement_val = { arith_op, lhs, size }
end
local cse_var = env[key]
if cse_var then
replacements[var] = cse_var
return visit(body, env)
else
env = dup(env)
env[key] = var
return { 'let', var, replacement_val, visit(body, env) }
end
elseif op == 'if' then
return { 'if', visit(expr[2], env), visit(expr[3], env), visit(expr[4], env) }
elseif simple[op] then
return expr
elseif op == 'call' then
local out = { 'call', expr[2] }
for i=3,#expr do table.insert(out, visit(expr[i], env)) end
return out
else
assert(relops[op])
return { op, visit(expr[2], env), visit(expr[3], env) }
end
end
return visit(expr, {})
end
local function inline_single_use_variables(expr)
local counts, substs = {}, {}
local function count(expr)
if expr == 'len' then return
elseif type(expr) == 'number' then return
elseif type(expr) == 'string' then counts[expr] = counts[expr] + 1
else
assert(type(expr) == 'table')
local op = expr[1]
if op == 'if' then
count(expr[2])
count(expr[3])
count(expr[4])
elseif op == 'let' then
counts[expr[2]] = 0
count(expr[3])
count(expr[4])
elseif relops[op] then
count(expr[2])
count(expr[3])
elseif unops[op] then
count(expr[2])
elseif binops[op] then
count(expr[2])
count(expr[3])
elseif simple[op] then
elseif op == 'call' then
for i=3,#expr do count(expr[i]) end
else
assert(op == '[]')
count(expr[2])
end
end
end
local function lookup(expr)
return substs[expr] or expr
end
local function subst(expr)
if type(expr) == 'number' then return expr end
if type(expr) == 'string' then return lookup(expr) end
local op = expr[1]
if op == 'let' then
local var, val, body = expr[2], expr[3], expr[4]
assert(type(val) == 'table')
local arith_op = val[1]
local replacement_val
if unops[arith_op] then
local lhs = subst(val[2])
replacement_val = { arith_op, lhs }
elseif binops[arith_op] then
local lhs, rhs = subst(val[2]), subst(val[3])
replacement_val = { arith_op, lhs, rhs }
else
assert(arith_op == '[]')
local lhs, size = subst(val[2]), val[3]
replacement_val = { arith_op, lhs, size }
end
if counts[var] == 1 then
substs[var] = replacement_val
return subst(body)
else
return { 'let', var, replacement_val, subst(body) }
end
elseif op == 'if' then
return { 'if', subst(expr[2]), subst(expr[3]), subst(expr[4]) }
elseif simple[op] then
return expr
elseif op == 'call' then
local out = { 'call', expr[2] }
for i=3,#expr do table.insert(out, subst(expr[i])) end
return out
else
assert(relops[op])
return { op, subst(expr[2]), subst(expr[3]) }
end
end
count(expr)
return subst(expr)
end
local function renumber(expr)
local count, substs = 0, {}
local function intern(var)
count = count + 1
local fresh = 'v'..count
substs[var] = fresh
return fresh
end
local function lookup(var)
if var == 'len' then return var end
-- NB: assert returns all its arguments on success, and we only
-- want to return the looked up variable here
local subst = assert(substs[var], "unbound variable: "..var)
return subst
end
local function visit(expr)
if type(expr) == 'number' then return expr end
if type(expr) == 'string' then return lookup(expr) end
local op = expr[1]
if op == 'let' then
local var, val, body = expr[2], expr[3], expr[4]
local fresh = intern(var)
return { 'let', fresh, visit(val), visit(body) }
elseif op == 'if' then
return { 'if', visit(expr[2]), visit(expr[3]), visit(expr[4]) }
elseif simple[op] then
return expr
elseif op == 'call' then
local out = { 'call', expr[2] }
for i=3,#expr do table.insert(out, visit(expr[i])) end
return out
elseif relops[op] then
return { op, visit(expr[2]), visit(expr[3]) }
elseif unops[op] then
return { op, visit(expr[2]) }
elseif binops[op] then
return { op, visit(expr[2]), visit(expr[3]) }
else
assert(op == '[]')
return { op, visit(expr[2]), expr[3] }
end
end
return visit(expr)
end
function convert_anf(expr)
return renumber(inline_single_use_variables(cse(lower(expr))))
end
function selftest()
local parse = require('pf.parse').parse
local expand = require('pf.expand').expand
local optimize = require('pf.optimize').optimize
local function test(expr)
return convert_anf(optimize(expand(parse(expr), "EN10MB")))
end
print("selftest: pf.anf")
test("tcp port 80")
print("OK")
end