lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> void luaK_patchtohere (FuncState *fs, int list) {
> luaK_getlabel(fs);
> luaK_concat(fs, &fs->jpc, list);
> }
> 
> 
> int luaK_jump (FuncState *fs) {
> int jpc = fs->jpc;  /* save list of jumps to here */
> int j;
> fs->jpc = NO_JUMP;
> j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
> luaK_concat(fs, &j, jpc);  /* keep them on hold */
> return j;
> }

The point is that Lua does an optimization here. When we call
luaK_patchtohere, we do not know yet what instruction is next.
If that instruction happens do be a jump, it would be silly to
jump to a jump; it would be better if the jumps in the luaK_patchtohere
could jump straight to the new-jump target.

So, patchtohere only concats the jumps in the list 'jpc' (the list of
pending jumps to the current 'pc'). If the next instruction is not
a jump, then that list is actually patched to jump to the current
position (see dischargejpc). But, if the new instruction is a jump,
then the list jpc is concatenated to the new-jump list (see luaK_jump),
so that when the new jump is patched those old jumps will be patched
to the same target.

-- Roberto