-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_matchers.lua
377 lines (318 loc) · 9.37 KB
/
ast_matchers.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
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
local AstMatcher = {}
local function is(ast, name) return ast.name == name end
local function is_literal(ast) return is(ast, "literal") end
local function is_identifier(ast) return is(ast, "identifier") end
local function is_stack_access(ast) return is(ast, "stack_access") end
local function is_literal_tbl_at(ast)
return is(ast, "table_at")
and (is_identifier(ast.tbl) or is_literal(ast.tbl))
and (is_identifier(ast.key) or is_literal(ast.key))
end
local function is_const(ast)
return is_identifier(ast)
or is_literal(ast)
or is_literal_tbl_at(ast)
end
local function is_push_const(ast)
return is(ast, "push") and is_const(ast.item)
end
local function OR(...)
local fs = {...}
return function(ast)
local result = false
for i, f in ipairs(fs) do
result = result or f(ast)
end
return result
end
end
local function NOT(f)
return function(ast)
return not f(ast)
end
end
local function is_stack_op(op)
return function(ast)
return is(ast, "stack_op") and ast.op == op
end
end
local function is_init_local(ast)
return is(ast, "init_local") and is_stack_access(ast.val)
end
local function is_push_binop(ast)
return is(ast, "push") and is(ast.item, "bin_op")
end
local function is_push_binop_pop(ast)
return is_push_binop(ast)
and is_stack_access(ast.item.p1)
and is_stack_access(ast.item.p2)
end
local function is_push_unop(ast)
return is(ast, "push") and is(ast.item, "unary_op")
end
local function is_push_unop_pop(ast)
return is_push_unop(ast) and is_stack_access(ast.item.p1)
end
local function is_tbl_at(ast)
return is(ast, "push")
and is(ast.item, "table_at")
and is_stack_access(ast.item.tbl)
and is_stack_access(ast.item.key)
end
local function is_tbl_put(ast)
return is(ast, "table_put") -- no push here
and is_stack_access(ast.tbl)
and is_stack_access(ast.key)
end
local function is_assignment(ast)
return is(ast, "assignment") and is_stack_access(ast.exp)
end
local function is_if(ast)
return is(ast, "if") and is_stack_access(ast.cond)
end
function AstMatcher:new(name, parts)
local obj = {name = name, parts = parts, logging = false}
setmetatable(obj, self)
self.__index = self
return obj
end
function AstMatcher:matches(ast, start)
for i, matcher in ipairs(self.parts) do
if start + i -1 > #ast then return false end
local node = ast[start + i -1]
if not matcher(node) then
return false
end
end
return true
end
function AstMatcher:optimize(ast, i, result)
error("not implemented")
end
function AstMatcher:log(message)
if self.logging then
print("[OPTI] " .. self.name .. ": " .. message)
end
end
function AstMatcher:size()
return #self.parts
end
AtParamsInline = AstMatcher:new()
AtParamsInlineP2 = AstMatcher:new()
PutParamsInline = AstMatcher:new()
IfCondInline = AstMatcher:new()
AssignmentInline = AstMatcher:new()
UnaryInline = AstMatcher:new()
DupUnaryInline = AstMatcher:new()
DupBinaryInline = AstMatcher:new()
DupDupBinaryInline = AstMatcher:new()
DupIfInline = AstMatcher:new()
BinaryInline = AstMatcher:new()
BinaryInlineP2 = AstMatcher:new()
InlineInitLocalConst = AstMatcher:new()
--[[
Inline table at parameters
1.) t 4 at => PUSH(t[4])
]]--
function AtParamsInline:optimize(ast, i, result)
self:log("inlining tbl at params")
local tbl, idx, op = ast[i], ast[i + 1], ast[i + 2]
op.item.tbl = tbl.item
op.item.key = idx.item
table.insert(result, op)
end
--[[
Inline table at index parameter
1.) ... 4 at => PUSH(POP[4])
]]--
function AtParamsInlineP2:optimize(ast, i, result)
self:log("inlining tbl at 2nd param")
local tbl, idx, op = ast[i], ast[i + 1], ast[i + 2]
if op.item.tbl.name == "stack_access" and
op.item.tbl.op == "pop2nd"
then
op.item.tbl.op = "pop"
end
op.item.key = idx.item
table.insert(result, tbl)
table.insert(result, op)
end
--[[
Inline table put parameters
1.) t 4 "abc" put => t[4]="abc"
]]--
function PutParamsInline:optimize(ast, i, result)
self:log("inlining tbl put params")
local tbl, key, val, op = ast[i], ast[i + 1], ast[i + 2], ast[i + 3]
op.tbl = tbl.item
op.key = key.item
op.value = val.item
table.insert(result, op)
end
--[[
Inline IF conditional
1.) false not IF ... THEN => IF not false THEN ... END
2.) 10 v < IF ... THEN => IF 10 < v THEN ... END
3.) v IF ... THEN => IF v THEN ... END
]]--
function IfCondInline:optimize(ast, i, result)
self:log("inlining if condition")
local cond, _if = ast[i], ast[i + 1]
_if.cond = cond.item
table.insert(result, _if)
end
--[[
Inline assignment operator's value operand
1.) 123 -> v => v = 123
]]--
function AssignmentInline:optimize(ast, i, result)
self:log("inlining assignment operator's param")
local p1, op = ast[i], ast[i + 1]
op.exp = p1.item
table.insert(result, op)
end
--[[
Inline unary operator's constant param
1.) false not => PUSH(not false)
]]--
function UnaryInline:optimize(ast, i, result)
self:log("inlining unary operator's param")
local p1, op = ast[i], ast[i + 1]
op.item.p1 = p1.item
table.insert(result, op)
end
--[[
Inline DUP followed by unary operator
1.) [ 1 2 3 ] DUP size => PUSH(#TOS)
]]--
function DupUnaryInline:optimize(ast, i, result)
self:log("inlining dup before unary operator")
local p1, op = ast[i], ast[i + 1]
op.item.p1.op = "tos"
op.item.p1.name = "stack_op" -- replace stack_access to stack_os, to prevent further inlining
table.insert(result, op)
end
--[[
Inline DUP followed by binary operator
1.) 3 DUP * => PUSH(TOS * POP)
]]--
function DupBinaryInline:optimize(ast, i, result)
self:log("inlining dup before binary operator")
local p1, p2, op = ast[i], ast[i + 1], ast[i + 2]
op.item.p1.op = "tos"
op.item.p1.name = "stack_op" -- replace stack_access to stack_os, to prevent further inlining
table.insert(result, p1)
table.insert(result, op)
end
--[[
Inline DUPs followed by binary operator
1.) DUP DUP * => PUSH(TOS * TOS)
]]--
function DupDupBinaryInline:optimize(ast, i, result)
self:log("inlining dup dup before binary operator")
local p1, p2, op = ast[i], ast[i + 1], ast[i + 2]
op.item.p1.op = "tos"
op.item.p2.op = "tos"
op.item.p1.name = "stack_op" -- replace stack_access to stack_os, to prevent further inlining
op.item.p2.name = "stack_op" -- replace stack_access to stack_os, to prevent further inlining
table.insert(result, op)
end
--[[
Inline DUP followed by IF
1.) DUP IF .. THEN => TOS IF .. THEN
]]--
function DupIfInline:optimize(ast, i, result)
self:log("inlining dup before if")
local dup, _if = ast[i], ast[i + 1]
_if.cond.op = "tos"
_if.cond.name = "stack_op" -- replace stack_access to stack_os, to prevent further inlining
table.insert(result, _if)
end
--[[
Inline binary operator's ALL constant operands
1.) 12 45 + => PUSH(12 + 45)
2.) 12 v1 + => PUSH(12 + v1)
3.) t 1 at 45 + => PUSH(t[1] + 45)
]]--
function BinaryInline:optimize(ast, i, result)
self:log("inlining binary operator params")
local p1, p2, op = ast[i], ast[i + 1], ast[i + 2]
op.item.p1 = p1.item
op.item.p2 = p2.item
table.insert(result, op)
end
--[[
Inline binary operator's 2nd, constant operand
Additonally replace DUP with DROP if applicable
1.) 123 + => PUSH(POP + 123)
2.) DUP 123 + => PUSH(TOS + 123)
]]--
function BinaryInlineP2:optimize(ast, i, result)
self:log("inlining binary operator's 2nd param")
local p1, p2, op = ast[i], ast[i + 1], ast[i + 2]
if op.item.p1.name == "stack_access" then
if op.item.p1.op == "pop2nd" then
op.item.p1.op = "pop"
end
if is_stack_op("dup")(p1) then
op.item.p1.op = "tos" -- inline if dup
end
end
op.item.p2 = p2.item -- inline const param
if not is_stack_op("dup")(p1) then -- dup was inlined skip it
table.insert(result, p1)
end
table.insert(result, op)
end
--[[
Used internally
]]--
function InlineInitLocalConst:optimize(ast, i, result)
self:log("inlining init local constant")
local p1, init_local = ast[i], ast[i + 1]
init_local.val = p1.item
table.insert(result, init_local)
end
return {
PutParamsInline:new(
"inline put params ",
{is_push_const, is_push_const, is_push_const, is_tbl_put}),
AtParamsInline:new(
"inline at params",
{is_push_const, is_push_const, is_tbl_at}),
AtParamsInlineP2:new(
"inline at p2",
{NOT(is_push_const), is_push_const, is_tbl_at}),
BinaryInline:new(
"binary inline",
{is_push_const, is_push_const, is_push_binop_pop}),
BinaryInlineP2:new(
"binary p2 inline",
{NOT(is_push_const), is_push_const, is_push_binop_pop}),
UnaryInline:new(
"unary inline",
{is_push_const, is_push_unop_pop}),
DupUnaryInline:new(
"dup unary inline",
{is_stack_op("dup"), is_push_unop_pop}),
DupDupBinaryInline:new(
"dup dup binary inline",
{is_stack_op("dup"), is_stack_op("dup"), is_push_binop_pop}),
DupBinaryInline:new(
"dup binary inline",
{NOT(is_stack_op("dup")), is_stack_op("dup"), is_push_binop_pop}),
DupIfInline:new(
"dup if inline",
{is_stack_op("dup"), is_if}),
AssignmentInline:new(
"assignment inline",
{is_push_const, is_assignment}),
IfCondInline:new(
"if cond inline",
{OR(is_push_const,
is_push_unop,
is_push_binop), is_if}), -- TODO 10 3 over < if
InlineInitLocalConst:new(
"inline init local const", -- only optimizes one parameter
{is_push_const, is_init_local}),
}