Skip to content

Commit

Permalink
Merge pull request #521 from pallene-lang/init-upvalues
Browse files Browse the repository at this point in the history
Rename SetUpvalues to InitUpvalues
  • Loading branch information
hugomg authored Apr 22, 2022
2 parents 9315ecb + 3442959 commit bf41939
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pallene/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ gen_cmd["NewClosure"] = function (self, cmd, _func)
})
end

gen_cmd["SetUpvalues"] = function(self, cmd, _func)
gen_cmd["InitUpvalues"] = function(self, cmd, _func)
local func = self.module.functions[cmd.f_id]

assert(cmd.src_f._tag == "ir.Value.LocalVar")
Expand Down
16 changes: 8 additions & 8 deletions pallene/constant_propagation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ function constant_propagation.run(module)
f_data.locvar_constant_init[id] = cmd.src
end

elseif tag == "ir.Cmd.SetUpvalues" then
elseif tag == "ir.Cmd.InitUpvalues" then
for u_id, value in ipairs(cmd.srcs) do
local next_f = data_of_func[cmd.f_id]
if value._tag == "ir.Value.LocalVar" then
local const_init = f_data.locvar_constant_init[value.id]
next_f.constant_val_of_upvalue[u_id] = const_init

elseif value._tag == "ir.Value.Upvalue" then
-- A `NewClosure` or `SetUpvalues` instruction can only reference values in outer scopes,
-- which exist in surrounding functions that have a numerically lesser `f_id`.
-- Due to this, we can reliable tie the constant initializer of an inner upvalue in a nested
-- function to the constantant initializer of the outer upvalue that it captures.
-- A `NewClosure` or `InitUpvalues` instruction can only reference values in
-- outer scopes. That is, from a surrounding functions with a smaller f_id.
-- Due to this, if the outer variable is initialized to a constant, we can
-- reliable tie that to the upvalue we are seeing right now.
local const_init = f_data.constant_val_of_upvalue[value.id]
next_f.constant_val_of_upvalue[u_id] = const_init

Expand All @@ -127,7 +127,7 @@ function constant_propagation.run(module)

for cmd in ir.iter(func.body) do
local tag = cmd._tag
if tag == "ir.Cmd.SetUpvalues" then
if tag == "ir.Cmd.InitUpvalues" then
local next_f = assert(data_of_func[cmd.f_id])
for u_id, value in ipairs(cmd.srcs) do
if value._tag == "ir.Value.LocalVar" then
Expand Down Expand Up @@ -164,7 +164,7 @@ function constant_propagation.run(module)
-- With this loop, we assert this assumption.
for cmd in ir.iter(func.body) do
local tag = cmd._tag
if tag == "ir.Cmd.SetUpvalues" then
if tag == "ir.Cmd.InitUpvalues" then
local next_f = assert(data_of_func[cmd.f_id])
for u_id, value in ipairs(cmd.srcs) do
if value._tag == "ir.Value.LocalVar" and next_f.constant_val_of_upvalue[u_id] then
Expand All @@ -179,7 +179,7 @@ function constant_propagation.run(module)
-- 3) Remove propagated upvalues from the capture list.
for _, func in ipairs(module.functions) do
for cmd in ir.iter(func.body) do
if cmd._tag == "ir.Cmd.SetUpvalues" then
if cmd._tag == "ir.Cmd.InitUpvalues" then
local next_f = assert(data_of_func[cmd.f_id])
local ir_func = module.functions[cmd.f_id]
local new_u_id = next_f.new_upvalue_id
Expand Down
6 changes: 3 additions & 3 deletions pallene/ir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ local ir_cmd_constructors = {
SetField = {"loc", "rec_typ", "src_rec", "field_name", "src_v"},

-- Functions
-- note: the reason NewClosure and SetUpvalues are separate operations is so
-- note: the reason NewClosure and InitUpvalues are separate operations is so
-- we can create self-referential closures, for recursion or mutual recursion.
NewClosure = {"loc", "dst" , "f_id"},
SetUpvalues = {"loc", "src_f", "srcs", "f_id"},
NewClosure = {"loc", "dst", "f_id"},
InitUpvalues = {"loc", "src_f", "srcs", "f_id"},

-- (dst is false if the return value is void, or unused)
CallStatic = {"loc", "f_typ", "dsts", "src_f", "srcs"},
Expand Down
4 changes: 2 additions & 2 deletions pallene/print_ir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ local function Cmd(cmd)
if tag == "ir.Cmd.SetArr" then lhs = Bracket(cmd.src_arr, cmd.src_i)
elseif tag == "ir.Cmd.SetTable" then lhs = Bracket(cmd.src_tab, cmd.src_k)
elseif tag == "ir.Cmd.SetField" then lhs = Field(cmd.src_rec, cmd.field_name)
elseif tag == "ir.Cmd.SetUpvalues" then lhs = Val(cmd.src_f) .. ".upvalues"
elseif tag == "ir.Cmd.InitUpvalues" then lhs = Val(cmd.src_f) .. ".upvalues"
else
lhs = comma_concat(Vars(ir.get_dsts(cmd)))
end
Expand All @@ -218,7 +218,7 @@ local function Cmd(cmd)
elseif tag == "ir.Cmd.GetField" then rhs = Field(cmd.src_rec, cmd.field_name)
elseif tag == "ir.Cmd.SetField" then rhs = Val(cmd.src_v)
elseif tag == "ir.Cmd.NewClosure" then rhs = Call("NewClosure", { Fun(cmd.f_id) })
elseif tag == "ir.Cmd.SetUpvalues"then rhs = comma_concat(Vals(cmd.srcs))
elseif tag == "ir.Cmd.InitUpvalues" then rhs = comma_concat(Vals(cmd.srcs))
elseif tag == "ir.Cmd.CallStatic" then rhs = Call(Val(cmd.src_f), Vals(cmd.srcs))
elseif tag == "ir.Cmd.CallDyn" then rhs = Call(Val(cmd.src_f), Vals(cmd.srcs))
else
Expand Down
4 changes: 2 additions & 2 deletions pallene/to_ir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ function ToIR:convert_stat(cmds, stat)
for _, val in ipairs(captured_vars) do
table.insert(srcs, val)
end
table.insert(cmds, ir.Cmd.SetUpvalues(func.loc, src_f, srcs, f_id))
table.insert(cmds, ir.Cmd.InitUpvalues(func.loc, src_f, srcs, f_id))
end
end

Expand Down Expand Up @@ -997,7 +997,7 @@ function ToIR:exp_to_assignment(cmds, dst, exp)
for _, upval in ipairs(captured_vars) do
table.insert(srcs, upval)
end
table.insert(cmds, ir.Cmd.SetUpvalues(exp.loc, src_f, srcs, f_id))
table.insert(cmds, ir.Cmd.InitUpvalues(exp.loc, src_f, srcs, f_id))
end

elseif tag == "ast.Exp.ExtraRet" then
Expand Down

0 comments on commit bf41939

Please sign in to comment.