Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroflag committed Feb 7, 2025
1 parent 8e548a8 commit 7ff9ce5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
31 changes: 19 additions & 12 deletions src/ast_matchers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ local is_push_unop_pop = AND(is_push_unop, has_exp(has_exp(is_stack_consume)))
local is_dup = AND(has_name("stack_op"), has_op("dup"))
local is_2dup = AND(has_name("stack_op"), has_op("dup2"))
local is_over = AND(has_name("stack_op"), has_op("over"))
local has_p1_pop = has_p1(has_op("pop"))
local has_p2_pop = has_p2(has_op("pop"))

local is_push_binop_pop = AND(
has_name("push"),
Expand All @@ -88,17 +90,21 @@ local is_wrapped_binop_free_operand = AND(
has("exp", any),
has_exp(AND(
has_name("bin_op"),
OR(has_p1(has_op("pop")),
has_p2(has_op("pop"))))))

local is_push_non_destructive_op = OR(
AND(
is_push_binop,
OR(
AND(NOT(has_exp(has_p1(is_stack_consume)), NOT(has_exp(has_p2(is_stack_consume))))),
AND(has_exp(has_p1(AND(is_stack_consume, has_op("pop")))), NOT(has_exp(has_p2(is_stack_consume)))),
AND(has_exp(has_p2(AND(is_stack_consume, has_op("pop")))), NOT(has_exp(has_p1(is_stack_consume)))))),
AND(is_push_unop, NOT(has_exp(has_exp(is_stack_consume)))))
OR(has_p1_pop,
has_p2_pop))))

local inlined_push_unop = AND(
is_push_unop,
NOT(has_exp(has_exp(is_stack_consume))))

local inlined_push_binop = AND(
is_push_binop,
OR(
-- fully inlined
AND(NOT(has_exp(has_p1(is_stack_consume)), NOT(has_exp(has_p2(is_stack_consume))))),
-- partially inlined
AND(has_exp(has_p1_pop), NOT(has_exp(has_p2(is_stack_consume)))),
AND(has_exp(has_p2_pop), NOT(has_exp(has_p1(is_stack_consume))))))

local is_tbl_at = AND(
has_name("push"),
Expand Down Expand Up @@ -398,7 +404,8 @@ return {

BinaryConstBinaryInline:new(
"binary const binary inline",
{is_push_non_destructive_op,
{OR(inlined_push_binop,
inlined_push_unop),
is_wrapped_binop_free_operand}),

StackOpBinaryInline:new(
Expand Down
33 changes: 20 additions & 13 deletions src/equinox_bundle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ local is_push_unop_pop = AND(is_push_unop, has_exp(has_exp(is_stack_consume)))
local is_dup = AND(has_name("stack_op"), has_op("dup"))
local is_2dup = AND(has_name("stack_op"), has_op("dup2"))
local is_over = AND(has_name("stack_op"), has_op("over"))
local has_p1_pop = has_p1(has_op("pop"))
local has_p2_pop = has_p2(has_op("pop"))

local is_push_binop_pop = AND(
has_name("push"),
Expand All @@ -283,17 +285,21 @@ local is_wrapped_binop_free_operand = AND(
has("exp", any),
has_exp(AND(
has_name("bin_op"),
OR(has_p1(has_op("pop")),
has_p2(has_op("pop"))))))

local is_push_non_destructive_op = OR(
AND(
is_push_binop,
OR(
AND(NOT(has_exp(has_p1(is_stack_consume)), NOT(has_exp(has_p2(is_stack_consume))))),
AND(has_exp(has_p1(AND(is_stack_consume, has_op("pop")))), NOT(has_exp(has_p2(is_stack_consume)))),
AND(has_exp(has_p2(AND(is_stack_consume, has_op("pop")))), NOT(has_exp(has_p1(is_stack_consume)))))),
AND(is_push_unop, NOT(has_exp(has_exp(is_stack_consume)))))
OR(has_p1_pop,
has_p2_pop))))

local inlined_push_unop = AND(
is_push_unop,
NOT(has_exp(has_exp(is_stack_consume))))

local inlined_push_binop = AND(
is_push_binop,
OR(
-- fully inlined
AND(NOT(has_exp(has_p1(is_stack_consume)), NOT(has_exp(has_p2(is_stack_consume))))),
-- partially inlined
AND(has_exp(has_p1_pop), NOT(has_exp(has_p2(is_stack_consume)))),
AND(has_exp(has_p2_pop), NOT(has_exp(has_p1(is_stack_consume))))))

local is_tbl_at = AND(
has_name("push"),
Expand Down Expand Up @@ -593,7 +599,8 @@ return {

BinaryConstBinaryInline:new(
"binary const binary inline",
{is_push_non_destructive_op,
{OR(inlined_push_binop,
inlined_push_unop),
is_wrapped_binop_free_operand}),

StackOpBinaryInline:new(
Expand Down Expand Up @@ -3137,7 +3144,7 @@ return utils
end
end

__VERSION__="0.1-245"
__VERSION__="0.1-252"

local Compiler = require("compiler")
local Optimizer = require("ast_optimizer")
Expand Down
2 changes: 1 addition & 1 deletion src/version/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1-245
0.1-252

0 comments on commit 7ff9ce5

Please sign in to comment.