luaK_exp2anyregup in buildglobal (lparser.c) appears unnecessary

46 views
Skip to first unread message

Yan

unread,
Jan 15, 2026, 8:00:51 AM (6 days ago) Jan 15
to lua-l
In the `buildglobal` function, after singlevaraux returns:

```
// buildglobal function
singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
```

the resulting `var->k` for `ls->envn` is either VUPVAL or VLOCAL, and no control-flow jumps are involved.

* When var->k == VUPVAL, luaK_exp2anyregup has no effect.
* When var->k == VLOCAL, luaK_exp2anyregup also does nothing useful, because in the subsequent luaK_indexed, the register index is used directly via `t->u.var.ridx`:

```
// luaK_indexed function
t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
```

The same observation applies to Lua 5.4 as well.

Roberto Ierusalimschy

unread,
Jan 15, 2026, 9:46:12 AM (6 days ago) Jan 15
to lu...@googlegroups.com
> In the `buildglobal` function, after singlevaraux returns:
>
> ```
> // buildglobal function
> singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
> ```
>
> the resulting `var->k` for `ls->envn` is either VUPVAL or VLOCAL, and no
> control-flow jumps are involved.

_ENV can also be a global or a constant, as the code implies:

global _ENV, X; X = 1 -- _ENV is global here (VGLOBAL)

local <const> _ENV = 12 -- ENV is constant here (VCONST)

-- Roberto
Reply all
Reply to author
Forward
0 new messages