-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncstate.cpp
More file actions
462 lines (396 loc) · 14.8 KB
/
funcstate.cpp
File metadata and controls
462 lines (396 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
** $Id: lparser.c $
** Lua Parser - FuncState Methods
** See Copyright Notice in lua.h
*/
#define lparser_c
#define LUA_CORE
#include "lprefix.h"
#include <climits>
#include <cstring>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "llex.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
/* because all strings are unified by the scanner, the parser
can use pointer equality for string equality */
inline bool eqstr(const TString* a, const TString* b) noexcept {
return (a) == (b);
}
/*
** nodes for block list (list of active blocks)
*/
typedef struct BlockCnt {
struct BlockCnt *previous; /* chain */
int firstlabel; /* index of first label in this block */
int firstgoto; /* index of first pending goto in this block */
short nactvar; /* number of active declarations at block entry */
lu_byte upval; /* true if some variable in the block is an upvalue */
lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */
lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */
} BlockCnt;
inline bool hasmultret(expkind k) noexcept {
return (k) == VCALL || (k) == VVARARG;
}
typedef struct ConsControl {
expdesc v; /* last list item read */
expdesc *t; /* table descriptor */
int nh; /* total number of 'record' elements */
int na; /* number of array elements already stored */
int tostore; /* number of array elements pending to be stored */
int maxtostore; /* maximum number of pending elements */
} ConsControl;
l_noret FuncState::errorlimit(int limit, const char *what) {
lua_State *L = getLexState()->getLuaState();
const char *msg;
int line = getProto()->getLineDefined();
const char *where = (line == 0)
? "main function"
: luaO_pushfstring(L, "function at line %d", line);
msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
what, limit, where);
getLexState()->syntaxError(msg);
}
void FuncState::checklimit(int v, int l, const char *what) {
if (l_unlikely(v > l)) errorlimit(l, what);
}
/*
** Register a new local variable in the active 'Proto' (for debug
** information).
*/
short FuncState::registerlocalvar(TString *varname) {
Proto *proto = getProto();
int oldsize = proto->getLocVarsSize();
luaM_growvector(getLexState()->getLuaState(), proto->getLocVarsRef(), getNumDebugVars(), proto->getLocVarsSizeRef(),
LocVar, SHRT_MAX, "local variables");
auto locVarsSpan = proto->getDebugInfo().getLocVarsSpan();
while (oldsize < static_cast<int>(locVarsSpan.size()))
locVarsSpan[oldsize++].setVarName(nullptr);
locVarsSpan[getNumDebugVars()].setVarName(varname);
locVarsSpan[getNumDebugVars()].setStartPC(getPC());
luaC_objbarrier(getLexState()->getLuaState(), proto, varname);
return postIncrementNumDebugVars();
}
/*
** Return the "variable description" (Vardesc) of a given variable.
** (Unless noted otherwise, all variables are referred to by their
** compiler indices.)
*/
Vardesc *FuncState::getlocalvardesc(int vidx) {
return &getLexState()->getDyndata()->actvar()[getFirstLocal() + vidx];
}
/*
** Convert 'nvar', a compiler index level, to its corresponding
** register. For that, search for the highest variable below that level
** that is in a register and uses its register index ('ridx') plus one.
*/
lu_byte FuncState::reglevel(int nvar) {
while (nvar-- > 0) {
Vardesc *vd = getlocalvardesc(nvar); /* get previous variable */
if (vd->isInReg()) /* is in a register? */
return cast_byte(vd->vd.ridx + 1);
}
return 0; /* no variables in registers */
}
/*
** Return the number of variables in the register stack for the given
** function.
*/
lu_byte FuncState::nvarstack() {
return reglevel(getNumActiveVars());
}
/*
** Get the debug-information entry for current variable 'vidx'.
*/
LocVar *FuncState::localdebuginfo(int vidx) {
Vardesc *vd = getlocalvardesc(vidx);
if (!vd->isInReg())
return nullptr; /* no debug info. for constants */
else {
int idx = vd->vd.pidx;
lua_assert(idx < getNumDebugVars());
return &getProto()->getLocVars()[idx];
}
}
/*
** Create an expression representing variable 'vidx'
*/
void FuncState::init_var(expdesc *e, int vidx) {
e->setFalseList(NO_JUMP);
e->setTrueList(NO_JUMP);
e->setKind(VLOCAL);
e->setLocalVarIndex(cast_short(vidx));
e->setLocalRegister(getlocalvardesc(vidx)->vd.ridx);
}
/*
** Close the scope for all variables up to level 'tolevel'.
** (debug info.)
*/
void FuncState::removevars(int tolevel) {
int current_n = getLexState()->getDyndata()->actvar().getN();
getLexState()->getDyndata()->actvar().setN(current_n - (getNumActiveVars() - tolevel));
while (getNumActiveVars() > tolevel) {
LocVar *var = localdebuginfo(--getNumActiveVarsRef());
if (var) /* does it have debug information? */
var->setEndPC(getPC());
}
}
/*
** Search the upvalues of the function for one
** with the given 'name'.
*/
int FuncState::searchupvalue(TString *name) {
auto upvaluesSpan = getProto()->getUpvaluesSpan();
for (size_t i = 0; i < static_cast<size_t>(getNumUpvalues()); i++) {
if (eqstr(upvaluesSpan[i].getName(), name)) return static_cast<int>(i);
}
return -1; /* not found */
}
Upvaldesc *FuncState::allocupvalue() {
Proto *proto = getProto();
int oldsize = proto->getUpvaluesSize();
checklimit(getNumUpvalues() + 1, MAXUPVAL, "upvalues");
luaM_growvector(getLexState()->getLuaState(), proto->getUpvaluesRef(), getNumUpvalues(), proto->getUpvaluesSizeRef(),
Upvaldesc, MAXUPVAL, "upvalues");
auto upvaluesSpan = proto->getUpvaluesSpan();
while (oldsize < static_cast<int>(upvaluesSpan.size()))
upvaluesSpan[oldsize++].setName(nullptr);
return &upvaluesSpan[getNumUpvaluesRef()++];
}
int FuncState::newupvalue(TString *name, expdesc *v) {
Upvaldesc *up = allocupvalue();
FuncState *prevFunc = getPrev();
if (v->getKind() == VLOCAL) {
up->setInStack(1);
up->setIndex(v->getLocalRegister());
up->setKind(prevFunc->getlocalvardesc(v->getLocalVarIndex())->vd.kind);
lua_assert(eqstr(name, prevFunc->getlocalvardesc(v->getLocalVarIndex())->vd.name));
}
else {
up->setInStack(0);
up->setIndex(cast_byte(v->getInfo()));
up->setKind(prevFunc->getProto()->getUpvalues()[v->getInfo()].getKind());
lua_assert(eqstr(name, prevFunc->getProto()->getUpvalues()[v->getInfo()].getName()));
}
up->setName(name);
luaC_objbarrier(getLexState()->getLuaState(), getProto(), name);
return getNumUpvalues() - 1;
}
/*
** Look for an active variable with the name 'n' in the function.
** If found, initialize 'var' with it and return its expression kind;
** otherwise return -1. While searching, var->u.info==-1 means that
** the preambular global declaration is active (the default while
** there is no other global declaration); var->u.info==-2 means there
** is no active collective declaration (some previous global declaration
** but no collective declaration); and var->u.info>=0 points to the
** inner-most (the first one found) collective declaration, if there is one.
*/
int FuncState::searchvar(TString *n, expdesc *var) {
int nactive = static_cast<int>(getNumActiveVars());
int i;
for (i = nactive - 1; i >= 0; i--) {
Vardesc *vd = getlocalvardesc(i);
if (vd->isGlobal()) { /* global declaration? */
if (vd->vd.name == nullptr) { /* collective declaration? */
if (var->getInfo() < 0) /* no previous collective declaration? */
var->setInfo(getFirstLocal() + i); /* this is the first one */
}
else { /* global name */
if (eqstr(n, vd->vd.name)) { /* found? */
var->init(VGLOBAL, getFirstLocal() + i);
return VGLOBAL;
}
else if (var->getInfo() == -1) /* active preambular declaration? */
var->setInfo(-2); /* invalidate preambular declaration */
}
}
else if (eqstr(n, vd->vd.name)) { /* found? */
if (vd->vd.kind == RDKCTC) /* compile-time constant? */
var->init(VCONST, getFirstLocal() + i);
else /* local variable */
init_var(var, i);
return cast_int(var->getKind());
}
}
return -1; /* not found */
}
/*
** Mark block where variable at given level was defined
** (to emit close instructions later).
*/
void FuncState::markupval(int level) {
BlockCnt *block = getBlock();
while (block->nactvar > level)
block = block->previous;
block->upval = 1;
setNeedClose(1);
}
/*
** Mark that current block has a to-be-closed variable.
*/
void FuncState::marktobeclosed() {
BlockCnt *block = getBlock();
block->upval = 1;
block->insidetbc = 1;
setNeedClose(1);
}
/*
** Find a variable with the given name 'n'. If it is an upvalue, add
** this upvalue into all intermediate functions. If it is a global, set
** 'var' as 'void' as a flag.
*/
void FuncState::singlevaraux(TString *n, expdesc *var, int base) {
int v = searchvar(n, var); /* look up variables at current level */
if (v >= 0) { /* found? */
if (v == VLOCAL && !base)
markupval(var->getLocalVarIndex()); /* local will be used as an upval */
}
else { /* not found at current level; try upvalues */
int idx = searchupvalue(n); /* try existing upvalues */
if (idx < 0) { /* not found? */
if (getPrev() != nullptr) /* more levels? */
getPrev()->singlevaraux(n, var, 0); /* try upper levels */
if (var->getKind() == VLOCAL || var->getKind() == VUPVAL) /* local or upvalue? */
idx = newupvalue(n, var); /* will be a new upvalue */
else /* it is a global or a constant */
return; /* don't need to do anything at this level */
}
var->init(VUPVAL, idx); /* new or old upvalue */
}
}
/*
** Traverse the pending gotos of the finishing block checking whether
** each match some label of that block. Those that do not match are
** "exported" to the outer block, to be solved there. In particular,
** its 'nactvar' is updated with the level of the inner block,
** as the variables of the inner block are now out of scope.
*/
void FuncState::solvegotos(BlockCnt *blockCnt) {
LexState *lexState = getLexState();
Labellist *gl = &lexState->getDyndata()->gt;
int outlevel = reglevel(blockCnt->nactvar); /* level outside the block */
int igt = blockCnt->firstgoto; /* first goto in the finishing block */
while (igt < gl->getN()) { /* for each pending goto */
Labeldesc *gt = &(*gl)[igt];
/* search for a matching label in the current block */
Labeldesc *lb = lexState->findlabel(gt->name, blockCnt->firstlabel);
if (lb != nullptr) /* found a match? */
lexState->closegoto(this, igt, lb, blockCnt->upval); /* close and remove goto */
else { /* adjust 'goto' for outer block */
/* block has variables to be closed and goto escapes the scope of
some variable? */
if (blockCnt->upval && reglevel(gt->nactvar) > outlevel)
gt->close = 1; /* jump may need a close */
gt->nactvar = blockCnt->nactvar; /* correct level for outer block */
igt++; /* go to next goto */
}
}
lexState->getDyndata()->label.setN(blockCnt->firstlabel); /* remove local labels */
}
void FuncState::enterblock(BlockCnt *blk, lu_byte isloop) {
blk->isloop = isloop;
blk->nactvar = getNumActiveVars();
blk->firstlabel = getLexState()->getDyndata()->label.getN();
blk->firstgoto = getLexState()->getDyndata()->gt.getN();
blk->upval = 0;
/* inherit 'insidetbc' from enclosing block */
blk->insidetbc = (getBlock() != nullptr && getBlock()->insidetbc);
blk->previous = getBlock(); /* link block in function's block list */
setBlock(blk);
lua_assert(getFreeReg() == luaY_nvarstack(this));
}
void FuncState::leaveblock() {
BlockCnt *blk = getBlock();
LexState *lexstate = getLexState();
lu_byte stklevel = reglevel(blk->nactvar); /* level outside block */
if (blk->previous && blk->upval) /* need a 'close'? */
codeABC(OP_CLOSE, stklevel, 0, 0);
setFreeReg(stklevel); /* free registers */
removevars(blk->nactvar); /* remove block locals */
lua_assert(blk->nactvar == getNumActiveVars()); /* back to level on entry */
if (blk->isloop == 2) /* has to fix pending breaks? */
lexstate->createlabel(this, lexstate->getBreakName(), 0, 0);
solvegotos(blk);
if (blk->previous == nullptr) { /* was it the last block? */
if (blk->firstgoto < lexstate->getDyndata()->gt.getN()) /* still pending gotos? */
lexstate->undefgoto(this, &lexstate->getDyndata()->gt[blk->firstgoto]); /* error */
}
setBlock(blk->previous); /* current block now is previous one */
}
void FuncState::closelistfield(ConsControl *cc) {
lua_assert(cc->tostore > 0);
exp2nextreg(&cc->v);
cc->v.setKind(VVOID);
if (cc->tostore >= cc->maxtostore) {
setlist(cc->t->getInfo(), cc->na, cc->tostore); /* flush */
cc->na += cc->tostore;
cc->tostore = 0; /* no more items pending */
}
}
void FuncState::lastlistfield(ConsControl *cc) {
if (cc->tostore == 0) return;
if (hasmultret(cc->v.getKind())) {
setreturns(&cc->v, LUA_MULTRET);
setlist(cc->t->getInfo(), cc->na, LUA_MULTRET);
cc->na--; /* do not count last expression (unknown number of elements) */
}
else {
if (cc->v.getKind() != VVOID)
exp2nextreg(&cc->v);
setlist(cc->t->getInfo(), cc->na, cc->tostore);
}
cc->na += cc->tostore;
}
/*
** Compute a limit for how many registers a constructor can use before
** emitting a 'SETLIST' instruction, based on how many registers are
** available.
*/
int FuncState::maxtostore() {
int numfreeregs = MAX_FSTACK - getFreeReg();
if (numfreeregs >= 160) /* "lots" of registers? */
return numfreeregs / 5; /* use up to 1/5 of them */
else if (numfreeregs >= 80) /* still "enough" registers? */
return 10; /* one 'SETLIST' instruction for each 10 values */
else /* save registers for potential more nesting */
return 1;
}
void FuncState::setvararg(int nparams) {
getProto()->setFlag(getProto()->getFlag() | PF_ISVARARG);
codeABC(OP_VARARGPREP, nparams, 0, 0);
}
/* Create code to store the "top" register in 'var' */
void FuncState::storevartop(expdesc *var) {
expdesc e;
e.init(VNONRELOC, getFreeReg() - 1);
storevar(var, &e); /* will also free the top register */
}
/*
** Fix for instruction at position 'pcpos' to jump to 'dest'.
** (Jump addresses are relative in Lua). 'back' true means
** a back jump.
*/
void FuncState::fixforjump(int pcpos, int dest, int back) {
Instruction *jmp = &getProto()->getCode()[pcpos];
int offset = dest - (pcpos + 1);
if (back)
offset = -offset;
if (l_unlikely(offset > MAXARG_Bx))
getLexState()->syntaxError("control structure too long");
SETARG_Bx(*jmp, static_cast<unsigned int>(offset));
}
void FuncState::checktoclose(int level) {
if (level != -1) { /* is there a to-be-closed variable? */
marktobeclosed();
codeABC(OP_TBC, reglevel(level), 0, 0);
}
}