Skip to content

Commit d399bb8

Browse files
committed
Revert "Give variables the same names in ir and in lua"
This reverts commit c122bd1. The reverted commit breaks variables reusing in generated code (see sched_variables_helper()), so it was possible to reach 200 local variables limit when trying to compile a large schema. Added a test case. Fixes #124.
1 parent e90fe20 commit d399bb8

File tree

3 files changed

+582
-44
lines changed

3 files changed

+582
-44
lines changed

avro_schema/backend.lua

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ local random_bytes = digest.base64_decode([[
1919
X1CntcveBDc4inHKyWfyXw5iCjg1f3TmeX88pZ2Galb9tXpTt1uBO0pZrkU5NW1/4Ki9g8fAwElq
2020
B3dRsBscsg==]])
2121

22-
-- The procedure gives ids to variables which then used to generate its names.
23-
-- The general rule is to give the same number as it was in `ir` representation.
2422
local sched_variables_helper
2523
sched_variables_helper = function(block, n, varmap, reusequeue)
2624
for i = 1, #block do
@@ -33,7 +31,7 @@ sched_variables_helper = function(block, n, varmap, reusequeue)
3331
var = n + 1
3432
n = var
3533
end
36-
varmap[o.ipv] = o.ipv
34+
varmap[o.ipv] = var
3735
elseif o.op == opcode.ENDVAR then
3836
insert(reusequeue, varmap[o.ipv])
3937
end
@@ -598,43 +596,22 @@ emit_nested_block = function(ctx, block, cc, res)
598596
insert(queue, cc)
599597
end
600598

601-
-- The procedure emits local variables with names corresponding to their
602-
-- register numbers in `ir` representation
603-
local function emit_locals(varmap, nservice_fields, res)
604-
if nservice_fields then
605-
-- Allocate variables for service_fields like this:
606-
-- local sf1
607-
-- local sf2 ...
608-
for num = 1, nservice_fields do
609-
insert(res, format('local sf%d', num))
610-
end
611-
end
612-
local var_nums = {}
613-
for _, num in pairs(varmap) do
614-
table.insert(var_nums, num)
615-
end
616-
local pos = 0
617-
-- generate batches like: local x1, x2, x3, x4
618-
-- until it there are 4 or more variables still
619-
while pos < #var_nums - 4 do
620-
insert(res, format('local x%d, x%d, x%d, x%d',
621-
var_nums[pos + 1],
622-
var_nums[pos + 2],
623-
var_nums[pos + 3],
624-
var_nums[pos + 4]))
625-
pos = pos + 4
626-
end
627-
-- emit variables by one if less then 4 left
628-
while pos < #var_nums do
629-
insert(res, format('local x%d',
630-
var_nums[pos + 1]))
631-
pos = pos + 1
632-
end
633-
end
599+
local locals_tab = {
600+
[0] = 'local x%d',
601+
'local x%d, x%d',
602+
'local x%d, x%d, x%d'
603+
}
634604

635605
local function emit_func_body(il, func, nlocals_min, res)
636-
local _, varmap = sched_func_variables(func)
637-
emit_locals(varmap, nlocals_min, res)
606+
local nlocals, varmap = sched_func_variables(func)
607+
if nlocals_min and nlocals_min > nlocals then
608+
nlocals = nlocals_min
609+
end
610+
for i = 1, nlocals, 4 do
611+
insert(res, format(locals_tab[nlocals - i] or
612+
'local x%d, x%d, x%d, x%d',
613+
i, i+1, i+2, i+3))
614+
end
638615
if il.enable_loop_peeling then
639616
peel_annotate(func, 0)
640617
end

avro_schema/init.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,16 @@ local function gen_fetch_service_fields(service_fields)
250250
local code = {}
251251
for i, field in ipairs(service_fields) do
252252
if field == 'boolean' then
253-
insert(code, format('sf%d = r.t[%d] == 3', i, i))
253+
insert(code, format('x%d = r.t[%d] == 3', i, i))
254254
elseif field == 'int' then
255-
insert(code, format('sf%d = tonumber(r.v[%d].ival)', i, i))
255+
insert(code, format('x%d = tonumber(r.v[%d].ival)', i, i))
256256
elseif field == 'long' then
257-
insert(code, format('sf%d = r.v[%d].ival', i, i))
257+
insert(code, format('x%d = r.v[%d].ival', i, i))
258258
elseif field == 'float' or field == 'double' then
259-
insert(code, format('sf%d = r.v[%d].dval', i, i))
259+
insert(code, format('x%d = r.v[%d].dval', i, i))
260260
elseif field == 'string' or field == 'bytes' then
261261
insert(code, format([[
262-
sf%d = ffi_string(r.b1-r.v[%d].xoff, r.v[%d].xlen)]], i, i, i))
262+
x%d = ffi_string(r.b1-r.v[%d].xoff, r.v[%d].xlen)]], i, i, i))
263263
else
264264
assert(false)
265265
end
@@ -345,7 +345,7 @@ r = rt_regs; v0 = 0; v1 = 0
345345
msgpack_data = decode_proc(r, data)
346346
r.b2 = ffi_cast("const uint8_t *", cpool) + #cpool]],
347347
conversion_complete = concat(u_complete, '\n'),
348-
func_return = 'return v0' .. param_list(n, 'sf'),
348+
func_return = 'return v0' .. param_list(n, 'x'),
349349
iter_prolog = 'if _ < 16 then goto continue end' -- artificially bump iter count
350350
})
351351

0 commit comments

Comments
 (0)