From 2a6b326c01b44348534ca41022f1743f543db961 Mon Sep 17 00:00:00 2001 From: Sainan Date: Sat, 16 Dec 2023 05:54:45 +0100 Subject: [PATCH] Massively simplify luaK_prepcallfirstarg on the assumption that 'func' is actually not in a register yet, hence we can discharge it into a VRELOC, and put it directly in the correct register. Managing 'e' is also not a PITA under this assumption because we don't have to worry about the function being overwritten. --- src/lcode.cpp | 44 ++++++-------------------------------------- 1 file changed, 6 insertions(+), 38 deletions(-) diff --git a/src/lcode.cpp b/src/lcode.cpp index 7c5bf08f2a..8722e6dcbf 100644 --- a/src/lcode.cpp +++ b/src/lcode.cpp @@ -1104,44 +1104,12 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { ** Convert expression 'e' into 'func(e,'. */ void luaK_prepcallfirstarg (FuncState *fs, expdesc *e, expdesc *func) { - luaK_exp2anyreg(fs, func); - int freg = func->u.reg; /* register where 'func' was placed */ - if (e->k == VNIL || e->k == VFALSE || e->k == VTRUE || e->k == VKSTR || e->k == VK || e->k == VKFLT || e->k == VKINT || e->k == VRELOC) { /* 'e' is yet to be loaded into a register? */ - freeexp(fs, func); - const int basereg = fs->freereg; /* base register for call */ - exp2reg(fs, e, basereg + 1); - luaK_reserveregs(fs, 2); /* function and first arg */ - if (basereg != freg) { /* ensure function is in correct register */ - luaK_codeABC(fs, OP_MOVE, basereg, freg, 0); - } - e->u.reg = basereg; - e->k = VNONRELOC; /* expression has a fixed register */ - } - else { - luaK_exp2anyreg(fs, e); - int ereg = e->u.reg; /* register where 'e' was placed */ - freeexps(fs, e, func); - e->u.reg = fs->freereg; /* base register for call */ - e->k = VNONRELOC; /* expression has a fixed register */ - luaK_reserveregs(fs, 2); /* function and first arg */ - if (ereg == e->u.reg) { /* argument is in the register where the function should be? */ - if (freg == e->u.reg + 1) { /* function is in the register where the argument should be? */ - /* oh dear. we have to swap them around. move function into a temporary register. */ - luaK_checkstack(fs, 1); - int tmpreg = e->u.reg + 2; - luaK_codeABC(fs, OP_MOVE, tmpreg, freg, 0); - freg = tmpreg; - } - luaK_codeABC(fs, OP_MOVE, e->u.reg + 1, ereg, 0); /* move it where it should be */ - ereg = e->u.reg + 1; /* and don't do it again */ - } - if (e->u.reg != freg) { /* ensure function is in correct register */ - luaK_codeABC(fs, OP_MOVE, e->u.reg, freg, 0); - } - if (e->u.reg + 1 != ereg) { /* ensure argument is in correct register */ - luaK_codeABC(fs, OP_MOVE, e->u.reg + 1, ereg, 0); - } - } + luaK_reserveregs(fs, 2); /* function and first arg */ + const int basereg = fs->freereg; /* base register for call */ + luaK_exp2reg(fs, e, basereg + 1); + luaK_exp2reg(fs, func, basereg); + e->u.reg = basereg; + e->k = VNONRELOC; /* expression has a fixed register */ }