Skip to content

Commit 1029a21

Browse files
committed
Phase 122 (continued): Optimize compiler and core files with auto and declare-on-first-use
Optimized local variables in: - src/compiler/lcode.cpp: 18 conversions (offset, r1/r2, oldsize, k, n, etc.) - src/compiler/parser.cpp: 14 conversions (regLevel, vidx, needed, extra, pc, line, etc.) - src/core/lapi.cpp: 4 conversions (func, t, p, m) - src/core/ldo.cpp: 9 conversions (firstres, delta, ftransfer, fsize, nfixparams, narg) All changes use modern C++ auto type deduction for cleaner, more maintainable code. Tests pass. Performance: 4.88s avg (5 runs).
1 parent c6805cb commit 1029a21

File tree

4 files changed

+58
-58
lines changed

4 files changed

+58
-58
lines changed

src/compiler/lcode.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Instruction *FuncState::previousinstruction() {
8989
** a list of jumps.
9090
*/
9191
int FuncState::getjump(int position) {
92-
int offset = InstructionView(getProto()->getCode()[position]).sj();
92+
auto offset = InstructionView(getProto()->getCode()[position]).sj();
9393
if (offset == NO_JUMP) /* point to itself represents end of list */
9494
return NO_JUMP; /* end of list */
9595
else
@@ -101,8 +101,8 @@ int FuncState::getjump(int position) {
101101
** (Jump addresses are relative in Lua)
102102
*/
103103
void FuncState::fixjump(int position, int dest) {
104-
Instruction *jmp = &getProto()->getCode()[position];
105-
int offset = dest - (position + 1);
104+
auto *jmp = &getProto()->getCode()[position];
105+
auto offset = dest - (position + 1);
106106
lua_assert(dest != NO_JUMP);
107107
if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
108108
getLexState()->syntaxError("control structure too long");
@@ -125,7 +125,7 @@ int FuncState::condjump(OpCode o, int A, int B, int C, int k) {
125125
** unconditional.
126126
*/
127127
Instruction *FuncState::getjumpcontrol(int position) {
128-
Instruction *pi = &getProto()->getCode()[position];
128+
auto *pi = &getProto()->getCode()[position];
129129
if (position >= 1 && InstructionView(*(pi-1)).testTMode())
130130
return pi-1;
131131
else
@@ -140,7 +140,7 @@ Instruction *FuncState::getjumpcontrol(int position) {
140140
** no register value)
141141
*/
142142
int FuncState::patchtestreg(int node, int reg) {
143-
Instruction *i = getjumpcontrol(node);
143+
auto *i = getjumpcontrol(node);
144144
if (InstructionView(*i).opcode() != OP_TESTSET)
145145
return 0; /* cannot patch other instructions */
146146
if (reg != NO_REG && reg != InstructionView(*i).b())
@@ -169,7 +169,7 @@ int FuncState::removevalues(int list) {
169169
*/
170170
void FuncState::patchlistaux(int list, int vtarget, int reg, int dtarget) {
171171
while (list != NO_JUMP) {
172-
int next = getjump(list);
172+
auto next = getjump(list);
173173
if (patchtestreg(list, reg))
174174
fixjump(list, vtarget);
175175
else
@@ -189,8 +189,8 @@ void FuncState::patchlistaux(int list, int vtarget, int reg, int dtarget) {
189189
** Otherwise, store the difference from last line in 'lineinfo'.
190190
*/
191191
void FuncState::savelineinfo(Proto *proto, int line) {
192-
int linedif = line - getPreviousLine();
193-
int pcval = getPC() - 1; /* last instruction coded */
192+
auto linedif = line - getPreviousLine();
193+
auto pcval = getPC() - 1; /* last instruction coded */
194194
if (abs(linedif) >= LIMLINEDIFF || postIncrementInstructionsWithAbs() >= MAXIWTHABS) {
195195
luaM_growvector(getLexState()->getLuaState(), proto->getAbsLineInfoRef(), getNAbsLineInfo(),
196196
proto->getAbsLineInfoSizeRef(), AbsLineInfo, std::numeric_limits<int>::max(), "lines");
@@ -212,8 +212,8 @@ void FuncState::savelineinfo(Proto *proto, int line) {
212212
** absolute line info, too.
213213
*/
214214
void FuncState::removelastlineinfo() {
215-
Proto *proto = getProto();
216-
int pcval = getPC() - 1; /* last instruction coded */
215+
auto *proto = getProto();
216+
auto pcval = getPC() - 1; /* last instruction coded */
217217
if (proto->getLineInfo()[pcval] != ABSLINEINFO) { /* relative line info? */
218218
setPreviousLine(getPreviousLine() - proto->getLineInfo()[pcval]); /* correct last line saved */
219219
decrementInstructionsWithAbs(); /* undo previous increment */
@@ -238,7 +238,7 @@ void FuncState::removelastinstruction() {
238238
** Format and emit an 'iAsBx' instruction.
239239
*/
240240
int FuncState::codeAsBx(OpCode o, int A, int Bc) {
241-
int b = Bc + OFFSET_sBx;
241+
auto b = Bc + OFFSET_sBx;
242242
lua_assert(getOpMode(o) == OpMode::iAsBx);
243243
lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
244244
return code(CREATE_ABx(o, A, b));
@@ -261,7 +261,7 @@ int FuncState::codek(int reg, int k) {
261261
if (k <= MAXARG_Bx)
262262
return codeABx(OP_LOADK, reg, k);
263263
else {
264-
int p = codeABx(OP_LOADKX, reg, 0);
264+
auto p = codeABx(OP_LOADKX, reg, 0);
265265
codeextraarg(k);
266266
return p;
267267
}
@@ -306,8 +306,8 @@ void FuncState::freeExpression(expdesc *e) {
306306
** order.
307307
*/
308308
void FuncState::freeExpressions(expdesc *e1, expdesc *e2) {
309-
int r1 = (e1->getKind() == VNONRELOC) ? e1->getInfo() : -1;
310-
int r2 = (e2->getKind() == VNONRELOC) ? e2->getInfo() : -1;
309+
auto r1 = (e1->getKind() == VNONRELOC) ? e1->getInfo() : -1;
310+
auto r2 = (e2->getKind() == VNONRELOC) ? e2->getInfo() : -1;
311311
freeRegisters(r1, r2);
312312
}
313313

@@ -316,8 +316,8 @@ void FuncState::freeExpressions(expdesc *e1, expdesc *e2) {
316316
*/
317317
int FuncState::addk(Proto *proto, TValue *v) {
318318
lua_State *L = getLexState()->getLuaState();
319-
int oldsize = proto->getConstantsSize();
320-
int k = getNK();
319+
auto oldsize = proto->getConstantsSize();
320+
auto k = getNK();
321321
luaM_growvector(L, proto->getConstantsRef(), k, proto->getConstantsSizeRef(), TValue, MAXARG_Ax, "constants");
322322
auto constantsSpan = proto->getConstantsSpan();
323323
while (oldsize < static_cast<int>(constantsSpan.size()))
@@ -339,13 +339,13 @@ int FuncState::k2proto(TValue *key, TValue *v) {
339339
Proto *proto = getProto();
340340
LuaT tag = luaH_get(getKCache(), key, &val); /* query scanner table */
341341
if (!tagisempty(tag)) { /* is there an index there? */
342-
int k = cast_int(ivalue(&val));
342+
auto k = cast_int(ivalue(&val));
343343
/* collisions can happen only for float keys */
344344
lua_assert(ttisfloat(key) || luaV_rawequalobj(&proto->getConstants()[k], v));
345345
return k; /* reuse index */
346346
}
347347
else { /* constant not found; create a new entry */
348-
int k = addk(proto, v);
348+
auto k = addk(proto, v);
349349
/* cache it for reuse; numerical value does not need GC barrier;
350350
table is not a metatable, so it does not need to invalidate cache */
351351
val.setInt(k);
@@ -398,7 +398,7 @@ int FuncState::numberK(lua_Number r) {
398398
lua_Integer ik;
399399
kv.setFloat(k); /* key as a TValue */
400400
if (!luaV_flttointeger(k, &ik, F2Imod::F2Ieq)) { /* not an integer value? */
401-
int n = k2proto(&kv, &o); /* use key */
401+
auto n = k2proto(&kv, &o); /* use key */
402402
if (luaV_rawequalobj(&getProto()->getConstants()[n], &o)) /* correct value? */
403403
return n;
404404
}
@@ -575,7 +575,7 @@ int FuncState::code_loadbool(int A, OpCode op) {
575575
*/
576576
int FuncState::need_value(int list) {
577577
for (; list != NO_JUMP; list = getjump(list)) {
578-
Instruction i = *getjumpcontrol(list);
578+
auto i = *getjumpcontrol(list);
579579
if (InstructionView(i).opcode() != OP_TESTSET) return 1;
580580
}
581581
return 0; /* not found */
@@ -593,16 +593,16 @@ void FuncState::exp2reg(expdesc *e, int reg) {
593593
if (e->getKind() == VJMP) /* expression itself is a test? */
594594
concat(e->getTrueListRef(), e->getInfo()); /* put this jump in 't' list */
595595
if (hasjumps(e)) {
596-
int p_f = NO_JUMP; /* position of an eventual LOAD false */
597-
int p_t = NO_JUMP; /* position of an eventual LOAD true */
596+
auto p_f = NO_JUMP; /* position of an eventual LOAD false */
597+
auto p_t = NO_JUMP; /* position of an eventual LOAD true */
598598
if (need_value(e->getTrueList()) || need_value(e->getFalseList())) {
599-
int fj = (e->getKind() == VJMP) ? NO_JUMP : jump();
599+
auto fj = (e->getKind() == VJMP) ? NO_JUMP : jump();
600600
p_f = code_loadbool(reg, OP_LFALSESKIP); /* skip next inst. */
601601
p_t = code_loadbool(reg, OP_LOADTRUE);
602602
/* jump around these booleans if 'e' is not a test */
603603
patchtohere(fj);
604604
}
605-
int final = getlabel(); /* position after whole expression */
605+
auto final = getlabel(); /* position after whole expression */
606606
patchlistaux(e->getFalseList(), final, reg, p_f);
607607
patchlistaux(e->getTrueList(), final, reg, p_t);
608608
}
@@ -654,7 +654,7 @@ int FuncState::exp2RK(expdesc *e) {
654654
}
655655

656656
void FuncState::codeABRK(OpCode o, int A, int B, expdesc *ec) {
657-
int k = exp2RK(ec);
657+
auto k = exp2RK(ec);
658658
codeABCk(o, A, B, ec->getInfo(), k);
659659
}
660660

@@ -677,7 +677,7 @@ void FuncState::negatecondition(expdesc *e) {
677677
*/
678678
int FuncState::jumponcond(expdesc *e, int cond) {
679679
if (e->getKind() == VRELOC) {
680-
Instruction ie = getinstruction(this, e);
680+
auto ie = getinstruction(this, e);
681681
if (InstructionView(ie).opcode() == OP_NOT) {
682682
removelastinstruction(); /* remove previous OP_NOT */
683683
return condjump(OP_TEST, InstructionView(ie).b(), 0, 0, !cond);
@@ -848,7 +848,7 @@ static inline TMS binopr2TM (BinOpr opr) {
848848
** Expression to produce final result will be encoded in 'e'.
849849
*/
850850
void FuncState::codeunexpval(OpCode op, expdesc *e, int line) {
851-
int r = exp2anyreg(e); /* opcodes operate only on registers */
851+
auto r = exp2anyreg(e); /* opcodes operate only on registers */
852852
freeExpression(e);
853853
e->setInfo(codeABC(op, 0, r, 0)); /* generate opcode */
854854
e->setKind(VRELOC); /* all those operations are relocatable */
@@ -863,8 +863,8 @@ void FuncState::codeunexpval(OpCode op, expdesc *e, int line) {
863863
*/
864864
void FuncState::finishbinexpval(expdesc *e1, expdesc *e2, OpCode op, int v2,
865865
int flip, int line, OpCode mmop, TMS event) {
866-
int v1 = exp2anyreg(e1);
867-
int instrPos = codeABCk(op, 0, v1, v2, 0);
866+
auto v1 = exp2anyreg(e1);
867+
auto instrPos = codeABCk(op, 0, v1, v2, 0);
868868
freeExpressions(e1, e2);
869869
e1->setInfo(instrPos);
870870
e1->setKind(VRELOC); /* all those operations are relocatable */
@@ -878,8 +878,8 @@ void FuncState::finishbinexpval(expdesc *e1, expdesc *e2, OpCode op, int v2,
878878
** two registers.
879879
*/
880880
void FuncState::codebinexpval(BinOpr opr, expdesc *e1, expdesc *e2, int line) {
881-
OpCode op = binopr2op(opr, BinOpr::OPR_ADD, OP_ADD);
882-
int v2 = exp2anyreg(e2); /* make sure 'e2' is in a register */
881+
auto op = binopr2op(opr, BinOpr::OPR_ADD, OP_ADD);
882+
auto v2 = exp2anyreg(e2); /* make sure 'e2' is in a register */
883883
/* 'e1' must be already in a register or it is a constant */
884884
lua_assert((VNIL <= e1->getKind() && e1->getKind() <= VKSTR) ||
885885
e1->getKind() == VNONRELOC || e1->getKind() == VRELOC);

src/compiler/parser.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ void Parser::check_readonly(expdesc *e) {
309309
*/
310310
void Parser::adjustlocalvars(int nvars) {
311311
// FuncState passed as parameter
312-
int regLevel = fs->nvarstack();
312+
auto regLevel = fs->nvarstack();
313313
for (int i = 0; i < nvars; i++) {
314-
int vidx = fs->getNumActiveVarsRef()++;
314+
auto vidx = fs->getNumActiveVarsRef()++;
315315
Vardesc *var = fs->getlocalvardesc(vidx);
316316
var->vd.ridx = cast_byte(regLevel++);
317317
var->vd.pidx = fs->registerlocalvar(var->vd.name);
@@ -345,7 +345,7 @@ void Parser::buildvar(TString *varname, expdesc *var) {
345345
var->init(VGLOBAL, -1); /* global by default */
346346
fs->singlevaraux(varname, var, 1);
347347
if (var->getKind() == VGLOBAL) { /* global name? */
348-
int info = var->getInfo();
348+
auto info = var->getInfo();
349349
/* global by default in the scope of a global declaration? */
350350
if (info == -2)
351351
ls->semerror("variable '%s' not declared", getstr(varname));
@@ -369,9 +369,9 @@ void Parser::singlevar(expdesc *var) {
369369
*/
370370
void Parser::adjust_assign(int nvars, int nexps, expdesc *e) {
371371
// FuncState passed as parameter
372-
int needed = nvars - nexps; /* extra values needed */
372+
auto needed = nvars - nexps; /* extra values needed */
373373
if (hasmultret(e->getKind())) { /* last expression has multiple returns? */
374-
int extra = needed + 1; /* discount last expression itself */
374+
auto extra = needed + 1; /* discount last expression itself */
375375
if (extra < 0)
376376
extra = 0;
377377
fs->setreturns(e, extra); /* last exp. provides the difference */
@@ -391,7 +391,7 @@ void Parser::adjust_assign(int nvars, int nexps, expdesc *e) {
391391

392392
int Parser::newgotoentry(TString *name, int line) {
393393
// FuncState passed as parameter
394-
int pc = fs->jump(); /* create jump */
394+
auto pc = fs->jump(); /* create jump */
395395
fs->codeABC(OP_CLOSE, 0, 1, 0); /* spaceholder, marked as dead */
396396
return ls->newlabelentry(fs, &ls->getDyndata()->gt, name, line, pc);
397397
}
@@ -410,7 +410,7 @@ Proto *Parser::addprototype() {
410410
FuncState *funcstate = fs;
411411
Proto *proto = funcstate->getProto(); /* prototype of current function */
412412
if (funcstate->getNP() >= proto->getProtosSize()) {
413-
int oldsize = proto->getProtosSize();
413+
auto oldsize = proto->getProtosSize();
414414
luaM_growvector(state, proto->getProtosRef(), funcstate->getNP(), proto->getProtosSizeRef(), Proto *, MAXARG_Bx, "functions");
415415
auto protosSpan = proto->getProtosSpan();
416416
while (oldsize < static_cast<int>(protosSpan.size()))
@@ -607,8 +607,8 @@ void Parser::constructor( expdesc *table_exp) {
607607
/* constructor -> '{' [ field { sep field } [sep] ] '}'
608608
sep -> ',' | ';' */
609609
FuncState *funcstate = fs;
610-
int line = ls->getLineNumber();
611-
int pc = funcstate->codevABCk(OP_NEWTABLE, 0, 0, 0, 0);
610+
auto line = ls->getLineNumber();
611+
auto pc = funcstate->codevABCk(OP_NEWTABLE, 0, 0, 0, 0);
612612
ConsControl cc;
613613
funcstate->code(0); /* space for extra arg. */
614614
cc.na = cc.nh = cc.tostore = 0;
@@ -704,7 +704,7 @@ void Parser::funcargs( expdesc *f) {
704704
FuncState *funcstate = fs;
705705
expdesc args;
706706
int base, nparams;
707-
int line = ls->getLineNumber();
707+
auto line = ls->getLineNumber();
708708
switch (ls->getToken()) {
709709
case '(': { /* funcargs -> '(' [ explist ] ')' */
710710
ls->nextToken();
@@ -761,7 +761,7 @@ void Parser::primaryexp( expdesc *v) {
761761
/* primaryexp -> NAME | '(' expr ')' */
762762
switch (ls->getToken()) {
763763
case '(': {
764-
int line = ls->getLineNumber();
764+
auto line = ls->getLineNumber();
765765
ls->nextToken();
766766
expr(v);
767767
check_match( ')', '(', line);
@@ -1061,8 +1061,8 @@ void Parser::whilestat( int line) {
10611061
FuncState *funcstate = fs;
10621062
BlockCnt bl;
10631063
ls->nextToken(); /* skip WHILE */
1064-
int whileinit = funcstate->getlabel();
1065-
int condexit = cond();
1064+
auto whileinit = funcstate->getlabel();
1065+
auto condexit = cond();
10661066
funcstate->enterblock(&bl, 1);
10671067
checknext(static_cast<int>(RESERVED::TK_DO));
10681068
block();
@@ -1076,14 +1076,14 @@ void Parser::whilestat( int line) {
10761076
void Parser::repeatstat( int line) {
10771077
/* repeatstat -> REPEAT block UNTIL cond */
10781078
FuncState *funcstate = fs;
1079-
int repeat_init = funcstate->getlabel();
1079+
auto repeat_init = funcstate->getlabel();
10801080
BlockCnt bl1, bl2;
10811081
funcstate->enterblock(&bl1, 1); /* loop block */
10821082
funcstate->enterblock(&bl2, 0); /* scope block */
10831083
ls->nextToken(); /* skip REPEAT */
10841084
statlist();
10851085
check_match(static_cast<int>(RESERVED::TK_UNTIL), static_cast<int>(RESERVED::TK_REPEAT), line);
1086-
int condexit = cond(); /* read condition (inside scope block) */
1086+
auto condexit = cond(); /* read condition (inside scope block) */
10871087
funcstate->leaveblock(); /* finish scope */
10881088
if (bl2.upval) { /* upvalues? */
10891089
int exit = funcstate->jump(); /* normal exit must jump over fix */

src/core/lapi.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ LUA_API int lua_gettop (lua_State *L) {
130130
LUA_API void lua_settop (lua_State *L, int idx) {
131131
lua_lock(L);
132132
CallInfo *ci = L->getCI();
133-
StkId func = ci->funcRef().p;
133+
auto func = ci->funcRef().p;
134134
ptrdiff_t diff; /* difference for new top */
135135
if (idx >= 0) {
136136
api_check(L, idx <= ci->topRef().p - (func + 1), "new top too large");
@@ -187,11 +187,11 @@ static void reverse (lua_State *L, StkId from, StkId to) {
187187
*/
188188
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
189189
lua_lock(L);
190-
StkId t = L->getTop().p - 1; /* end of stack segment being rotated */
191-
StkId p = L->getStackSubsystem().indexToStack(L, idx); /* start of segment */
190+
auto t = L->getTop().p - 1; /* end of stack segment being rotated */
191+
auto p = L->getStackSubsystem().indexToStack(L, idx); /* start of segment */
192192
api_check(L, L->getTbclist().p < p, "moving a to-be-closed slot");
193193
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
194-
StkId m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
194+
auto m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
195195
reverse(L, p, m); /* reverse the prefix with length 'n' */
196196
reverse(L, m + 1, t); /* reverse the suffix */
197197
reverse(L, p, t); /* reverse the entire segment */

src/core/ldo.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,15 @@ void lua_State::hookCall(CallInfo *ci_arg) {
393393
void lua_State::retHook(CallInfo *ci_arg, int nres) {
394394
if (getHookMask() & LUA_MASKRET) { /* is return hook on? */
395395
lua_assert(getTop().p >= getStack().p + nres); /* ensure nres is in bounds */
396-
StkId firstres = getTop().p - nres; /* index of first result */
397-
int delta = 0; /* correction for vararg functions */
396+
auto firstres = getTop().p - nres; /* index of first result */
397+
auto delta = 0; /* correction for vararg functions */
398398
if (ci_arg->isLua()) {
399399
Proto *p = ci_arg->getFunc()->getProto();
400400
if (p->getFlag() & PF_ISVARARG)
401401
delta = ci_arg->getExtraArgs() + p->getNumParams() + 1;
402402
}
403403
ci_arg->funcRef().p += delta; /* if vararg, back to virtual 'func' */
404-
int ftransfer = cast_int(firstres - ci_arg->funcRef().p);
404+
auto ftransfer = cast_int(firstres - ci_arg->funcRef().p);
405405
callHook(LUA_HOOKRET, -1, ftransfer, nres); /* call it */
406406
ci_arg->funcRef().p -= delta;
407407
}
@@ -588,8 +588,8 @@ int lua_State::preTailCall(CallInfo *ci_arg, StkId func,
588588
return preCallC(func, status_val, fvalue(s2v(func)));
589589
case LuaT::LCL: { /* Lua function */
590590
Proto *p = clLvalue(s2v(func))->getProto();
591-
int fsize = p->getMaxStackSize(); /* frame size */
592-
int nfixparams = p->getNumParams();
591+
auto fsize = p->getMaxStackSize(); /* frame size */
592+
auto nfixparams = p->getNumParams();
593593
checkstackp(this, fsize - delta, func);
594594
ci_arg->funcRef().p -= delta; /* restore 'func' (if vararg) */
595595
for (int i = 0; i < narg1; i++) /* move down function and arguments */
@@ -637,9 +637,9 @@ CallInfo* lua_State::preCall(StkId func, int nresults) {
637637
case LuaT::LCL: { /* Lua function */
638638
CallInfo *ci_new;
639639
Proto *p = clLvalue(s2v(func))->getProto();
640-
int narg = cast_int(getTop().p - func) - 1; /* number of real arguments */
641-
int nfixparams = p->getNumParams();
642-
int fsize = p->getMaxStackSize(); /* frame size */
640+
auto narg = cast_int(getTop().p - func) - 1; /* number of real arguments */
641+
auto nfixparams = p->getNumParams();
642+
auto fsize = p->getMaxStackSize(); /* frame size */
643643
checkstackp(this, fsize, func);
644644
ci_new = setCI(prepareCallInfo(func, status_val, func + 1 + fsize));
645645
ci_new->setSavedPC(p->getCode()); /* starting point */

0 commit comments

Comments
 (0)